【问题标题】:How to ignore any particular column data from csv file using linux cli?如何使用 linux cli 忽略 csv 文件中的任何特定列数据?
【发布时间】:2018-10-20 11:28:56
【问题描述】:

我有 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

【问题讨论】:

标签: linux sorting awk grep cat


【解决方案1】:

我不确定cat the value of c1 c2 c3 c4 c5 and c9 是什么意思,但是如果您只想过滤那些列,那么您可以使用以下awk 命令:

awk 'BEGIN{OFS=FS=","}{print $1,$2,$3,$4,$5,$9}' sample.csv

输入:

more sample.csv 
c1,c2,c3,c4,c5,c6,c7,c8,c9
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

输出:

awk 'BEGIN{OFS=FS=","}{print $1,$2,$3,$4,$5,$9}' sample.csv 
c1,c2,c3,c4,c5,c9
123,B006195,T,O,INDIVIDUAL,NEW
12,C06195,T,O,INDIVIDUAL,NEW
12345,B00619,T,O,IND,OLD

说明:

您将, (BEGIN{OFS=FS=","}) 定义为字段分隔符(输入、输出),然后在将输出重定向到新的 csv 文件后,您只需为每一行打印需要显示{print $1,$2,$3,$4,$5,$9} 的列

如果您认为awk 对这个任务来说太过分了,那么您也可以只使用cut 命令(-d',' 是定义一个, 作为分隔符,-f... 是指定哪个字段需要保留):

$ cut -d',' -f1,2,3,4,5,9 sample.csv
c1,c2,c3,c4,c5,c9
123,B006195,T,O,INDIVIDUAL,NEW
12,C06195,T,O,INDIVIDUAL,NEW
12345,B00619,T,O,IND,OLD

【讨论】:

  • 谢谢,艾伦。是工作。我的意思是 cat file_name 来获取文件的输出。
【解决方案2】:

以下解决方案可能对您有所帮助,您需要在名为 fields 的变量 awk 中提供字段编号并可以打印出来。

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.

【讨论】:

  • 这是一个不错的原创解决方案 +1!! ;-)
猜你喜欢
  • 2019-09-27
  • 2020-06-02
  • 2014-08-14
  • 1970-01-01
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 2011-01-23
  • 1970-01-01
相关资源
最近更新 更多