Bash 剪切 .csv 文件

标签 bash csv cut

我正在使用示例 .csv 文件

my.csv:

1/2/09 6:17,Product1,1200,Mastercard,carolina,Basildon,England,United Kingdom,1/2/09 6:00,1/2/09 6:08,51.5,-1.1166667
1/2/09 4:53,Product1,1200,Visa,Betina,Parkville                   ,MO,United States,1/2/09 4:42,1/2/09 7:49,39.195,-94.68194
1/2/09 13:08,Product1,1200,Mastercard,Federica e Andrea,Astoria                     ,OR,United States,1/1/09 16:21,1/3/09 12:32,46.18806,-123.83
1/3/09 14:44,Product1,1200,Visa,Gouya,Echuca,Victoria,Australia,9/25/05 21:13,1/3/09 14:22,-36.1333333,144.75
1/4/09 12:56,Product2,3600,Visa,Gerd W ,Cahaba Heights              ,AL,United States,11/15/08 15:47,1/4/09 12:45,33.52056,-86.8025
1/4/09 13:19,Product1,1200,Visa,LAURENCE,Mickleton                   ,NJ,United States,9/24/08 15:19,1/4/09 13:04,39.79,-75.23806
1/4/09 20:11,Product1,1200,Mastercard,Fleur,Peoria                      ,IL,United States,1/3/09 9:38,1/4/09 19:45,40.69361,-89.58889
1/2/09 20:09,Product1,1200,Mastercard,adam,Martin                      ,TN,United States,1/2/09 17:43,1/4/09 20:01,36.34333,-88.85028
1/4/09 13:17,Product1,1200,Mastercard,Renee Elisabeth,Tel Aviv,Tel Aviv,Israel,1/4/09 13:03,1/4/09 22:10,32.0666667,34.7666667
1/4/09 14:11,Product1,1200,Visa,Aidan,Chatou,Ile-de-France,France,6/3/08 4:22,1/5/09 1:17,48.8833333,2.15

我正在尝试使用 cut -d","-f-6 my.csv 打印数据,但这仅打印第一行:
2009 年 1 月 2 日 6:17,产品 1,1200,万事达卡,卡罗来纳州,巴西尔登

如何让 cut 打印与分隔符/字段匹配的每一行的条目?

最佳答案

给出你的例子:

$ awk -F, '{print $6}' file
Basildon
Parkville                   
Astoria                     
Echuca
Cahaba Heights              
Mickleton                   
Peoria                      
Martin                      
Tel Aviv
Chatou

或者使用剪切:

$ cut -d, -f 6 file 
# same output

或者,对于第 6 个之前(包括第 6 个)的所有字段:

$ awk 'BEGIN{FS=OFS=","} {for (i=1;i<=6;i++) printf "%s%s", $i, i==6 ? ORS : OFS}' file

或者,剪切版本:

$ cut -d, -f-6 file 

关于Bash 剪切 .csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44422945/

相关文章:

bash - 如何将同一列粘贴n次?

bash - 如何在shell脚本中处理Ctrl + c?

bash - 从所有目录中删除 .svn 文件

php - 使用更新从 CSV 导入到 MySQL

json - 如何在 CLI 中运行 npm 包?

bash - cut: 可以设置多个空格作为分隔符吗?

bash - 使用 sed 将反斜杠替换为斜杠

mysql - 为什么MySQL Workbench表数据导入向导会导入0条记录?

linux - 从 `ls` 中删除前 3 列?

regex - 正则表达式从字符串中删除不需要的文本