Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a csv file looks like this:

site1.com,aaa,bbb,ccc
site2.com,qqq
site3.com,rrr,uuu,ppp
site4.com
site5.com,ddd,sss

I want to replace rows (lines) where the fourth column is empty. and I want to manipulate said rows by running a command on them. For example, with awk:

awk -F, '$4=="" {system("$1,cmd1 $1")}' test.csv

I then want to export as follows (output1 is output from cmd1 command):

site1.com,aaa,bbb,ccc
site2.com,output1
site3.com,rrr,uuu,ppp
site4.com,output1
site5.com,output1
share|improve this question
    
cmd1 should be run with the first field as argument, right? – Stéphane Chazelas 16 hours ago
    
I ignore my own advice occasionally but I strongly advise using a proper table editing tool here instead of awk (my own weapon of choice is R, but there are others, such as csvtool). – Konrad Rudolph 13 hours ago
awk -F, -v q=\' '
  function shellquote(s) {
    gsub(q, q "\\" q q, s)
    return q s q
  }
  $4 == "" {
    printf "%s,", $1
    system("cmd " shellquote($1))
    next
  }
  {print}'

The shellquote part is important. Consider the case of a value of $1 like ;reboot for instance.

share|improve this answer

You could use the following code:

awk -v output1="$(cmd1)" 'BEGIN{FS=OFS=","} NF&&NF<4{print $1,output1}1' file

The option -v set the variable output1 as the result of the shell command cmd1.

The statement BEGIN{FS=OFS=","} is setting the delimiter to the comma.

If the line is not empty and the number of fields is lower than 4, replace the line.

At last, the 1 triggers the default statement in awk: print the whole line.

share|improve this answer
awk -F, '{ if ( $4 == "" ){printf "%s,",$1 ; system(CMD) ;}else {print $0}}' test.csv

If the fourth filed is empty print the first field with the output of the command, else print the hole line.

But be aware of the output of the command how it will print

share|improve this answer

may be this post helps you http://stackoverflow.com/questions/20646819/how-can-i-pass-variables-from-awk-to-a-shell-command

$ cat input.txt
a,b,c,d
a,b
a,b,c,d
a,b,c
a,b,c,d


$ awk -F, 'NF<4{cmd="echo output1";while(cmd|getline line){printf("%s,%s\n",$0,line)};close(cmd);next}1' input.txt
a,b,c,d
a,b,output1
a,b,c,d
a,b,c,output1
a,b,c,d
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.