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

こ雲淡風輕ζ 提交于 2019-12-25 01:34:55

问题


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.



来源:https://stackoverflow.com/questions/18244092/use-awk-to-process-a-csv-tab-delimited-line-by-line

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!