How to replace specific row using sed or awk based with run command

awksedtext processing

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

Best 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

Related Question