use awk to process a csv (tab delimited) line by l

2019-09-06 13:45发布

问题:

sorry if this is impossible to do with AWK, but this is something I can think of with AWK.

I have a CSV file (tab delimited) that has about 10 columns with a big header section. The content is like this:

col1 col2 col3 col4 col5 col6 col7 col8 col9 col10

I don't need to filter by col1-9, but only need to look at col10 line by line. The content in each row of col10 is like this

int1/int2: int3,int4: int5: int6

My filtering condition is: if int3 + int4 >= 30, I'll print it out to a new csv file, otherwise filter it out (no print).

Is this possible to do with AWK in combination with shell scripts (read line by line?)? thanks a lot for reading my question

回答1:

try this

awk -F'\t' -v OFS='\t' '{ t = $10
split(t,x,":")
split(x[2],a,",")
}(a[1]+a[2])>=30' oldcsv > newcsv

or shorter:

awk -F'\t' -v OFS='\t' '{t=$10; split(t,a,/[:,]/)}(a[3]+a[2])>=30' oldcsv > newcsv

didn't test, but should work, if the format of $10 is fixed.



标签: shell csv awk line