如何使用linux cli忽略csv文件中的任何特定列数据?

我有9列喜欢c1 c2 c3 c4 c5 c6 c7 c8 c9,我想关注的价值c1 c2 c3 c4 c5 and c9。


列具有CSV格式的以下数据。如何通过CLI在Linux中执行此操作?请帮忙


样本数据


123,B006195,T,O,INDIVIDUAL,25^5820200^,2018-04-25,13,NEW

12,C06195,T,O,INDIVIDUAL,25^5820200^,2018-04-25,13,NEW

12345,B00619,T,O,IND,25^5820200^,2018-04-25,13,OLD

我尝试使用 cat file.csv | awk '{print $1,$2,$3,$4,$5}' > newfile


慕慕森
浏览 353回答 2
2回答

牛魔王的故事

以下解决方案可能对您有所帮助,您需要在awknamed变量中提供字段编号fields并可以打印出来。awk -F, -v fields="1,2,3,4,5,9" 'BEGIN{num=split(fields, array,",")} {for(i=1;i<=num;i++){printf("%s%s",$array[i],i==num?ORS:OFS)}}' OFS=,&nbsp; &nbsp;Input_file现在也添加非单一衬里形式的解决方案。awk -F, -v fields="1,2,3,4,5,9" 'BEGIN{&nbsp; num=split(fields, array,",")}{&nbsp; for(i=1;i<=num;i++){&nbsp; &nbsp; printf("%s%s",$array[i],i==num?ORS:OFS)}}' OFS=,&nbsp; &nbsp;Input_file上面的代码说明:awk -F, -v fields="1,2,3,4,5,9" '&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ##Setting field seprator as comma here with -F. Setting variable named fields with values of fields which we need.BEGIN{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;##Starting BEGIN section here for awk which will be executed before reading the Input_file.&nbsp; num=split(fields, array,",")}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ##using split to split the variable fields into array named array and creating variable num which will have number of element of array.{&nbsp; for(i=1;i<=num;i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;##Starting a for loop here which starts from variable named i value from 1 to till value of variable num.&nbsp; &nbsp; printf("%s%s",$array[i],i==num?ORS:OFS)}}&nbsp; ##Printing value of array[i] and then $array[i] will print the field value in current line too. Then checking condition variable i value equal to variable num then print new line else print space with OFS.' OFS=,&nbsp; Input_file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ##Mentioning the Input_file name here.
打开App,查看更多内容
随时随地看视频慕课网APP