0

I have a file which looks like this:

SOME,DESCRIPTIONS,OF,THE,COLUMNS
0,93,50127,0,49592,1,49592,1
100,195,107336,0,106491,2,107077,2
200,298,178492,3,170578,24,172045,13
...
2590,0

And I'm trying to add a ratio column based on the 4th ($4) and 6th ($6) columns.

I've written:

awk -F "," 'NR==1{$9=",ratio"}NR>1{if($4==0) $9 = "10000"; else $9 = ($6)/($4)}' $FILE

As you can see, on the very last line, the output is malformed and missing columns, so running the command results in a division by zero error.

Is there some way to skip over malformed lines?

0

1 Answer 1

0
awk 'BEGIN  { FS=OFS="," }
NR==1       { $9="ratio" }
NR>1 && NF>3{ if($4==0) $9 =10000; else $9 =$6/$4 } 
{ print }' infile
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .