亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

慕慕森 2021-04-14 21:18:37
我有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,NEW12,C06195,T,O,INDIVIDUAL,25^5820200^,2018-04-25,13,NEW12345,B00619,T,O,IND,25^5820200^,2018-04-25,13,OLD我嘗試使用 cat file.csv | awk '{print $1,$2,$3,$4,$5}' > newfile
查看完整描述

2 回答

?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

以下解決方案可能對您有所幫助,您需要在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=,   Input_file

現在也添加非單一襯里形式的解決方案。


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=,   Input_file

上面的代碼說明:


awk -F, -v fields="1,2,3,4,5,9" '              ##Setting field seprator as comma here with -F. Setting variable named fields with values of fields which we need.

BEGIN{                                         ##Starting BEGIN section here for awk which will be executed before reading the Input_file.

  num=split(fields, array,",")}                ##using split to split the variable fields into array named array and creating variable num which will have number of element of array.

{

  for(i=1;i<=num;i++){                         ##Starting a for loop here which starts from variable named i value from 1 to till value of variable num.

    printf("%s%s",$array[i],i==num?ORS:OFS)}}  ##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=,  Input_file                            ##Mentioning the Input_file name here.



查看完整回答
反對 回復 2021-04-16
  • 2 回答
  • 0 關注
  • 386 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號