美文网首页
命令行工具:cut列操作

命令行工具:cut列操作

作者: 心智万花筒 | 来源:发表于2017-05-28 21:53 被阅读34次

列操作cut

面对较大CSV文件的时候,可以用列工具做简单操作。

以如下的一个student.csv为例子:

name gender score grade
David male 85 B
Michael female 90 A
Cammy male 88 A
Tom female 59 C

甄选列cut

CSV有很多列,可以用cut挑选出指定列。这里有几个有用的参数:

  • -d:field delimiter,字段分隔符;
  • -f:fields,指定字段;

常用操作:

  • cut -d',' -f1 filename:提取第一列,当,为字段分隔符时
  • cut -d',' -f1,3:提取第一个和第三个字段,当,为字段分割符时
  • cut -d':' -f2-4:提取第二到第四个字段,当:为字段分割符时
  • cut -d',' -f3 --complement student.csv:提取除第三列的其他列,当,为分隔符时

比如cut -d"," -f1,3 student.csv产生如下结果:

name score
David 85
Michael 90
Cammy 88
Tom 59

相关文章

网友评论

      本文标题:命令行工具:cut列操作

      本文链接:https://www.haomeiwen.com/subject/kmnhfxtx.html