bash 将 csv 文件的第一列解析为新文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2652494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 19:11:30  来源:igfitidea点击:

Parsing the first column of a csv file to a new file

bashcsvsedawkcut

提问by S1syphus

Operating System: OSX Method: From the command line, so using sed, cut, gawk, although preferably no installing modules.

操作系统:OSX 方法:从命令行,因此使用 sed、cut、gawk,但最好不要安装模块。

Essentially I am trying to take the first column of a csv file and parse it to a new file.

本质上,我试图获取 csv 文件的第一列并将其解析为一个新文件。

Example input file

示例输入文件

EXAMPLEfoo,60,6
EXAMPLEbar,30,6
EXAMPLE1,60,3
EXAMPLE2,120,6
EXAMPLE3,60,6
EXAMPLE4,30,6

Desire output

欲望输出

EXAMPLEfoo 
EXAMPLEbar
EXAMPLE1
EXAMPLE2
EXAMPLE3
EXAMPLE4

So I want the first column.

所以我想要第一列。

Here is what I have tried so far:

这是我迄今为止尝试过的:

awk -F"," '{print }' in.csv > out.txt

awk -F"," '{for (i=2;i<=NF;i++)}' in.csv > out.txt

awk -F"," 'BEGIN { OFS="," }' '{print }' in.csv > out.txt

cat in.csv | cut -d \, -f 1 > out.txt

None seem to work, either they just print the first line or nothing at all, so I would assume it's failing to read line by line.

似乎没有任何工作,他们要么只打印第一行,要么根本不打印,所以我认为它无法逐行读取。

回答by Thomas

Your last option works perfectly for me:

您的最后一个选项非常适合我:

$ cat > in.csv  # Then pasted the example input followed by Ctrl+D:
EXAMPLEfoo,60,6
EXAMPLEbar,30,6
EXAMPLE1,60,3
EXAMPLE2,120,6
EXAMPLE3,60,6
EXAMPLE4,30,6
[Ctrl+D]
$ cat in.csv | cut -d, -f1
EXAMPLEfoo
EXAMPLEbar
EXAMPLE1
EXAMPLE2
EXAMPLE3
EXAMPLE4

Maybe line endings are biting you here? If the file has DOS-style or even old-Mac-style line endings, this might cause strange behaviour. Try running file in.csvand see what it comes up with.

也许行尾在这里咬你?如果文件具有 DOS 风格甚至旧 Mac 风格的行尾,这可能会导致奇怪的行为。尝试运行file in.csv,看看它会出现什么。

$ file in.unix.csv
in.unix.csv: ASCII text
$ file in.dos.csv
in.dos.csv: ASCII text, with CRLF line terminators

If the latter is your situation, use the dos2unixtool to convert the file.

如果您的情况是后者,请使用该dos2unix工具转换文件。

Edit: On OS X, it seems flipis what you want.

编辑:在 OS X 上,它似乎flip是你想要的

回答by Personman

I copy-pasted your sample input, saved it as in.csv, and then ran your first line,

我复制粘贴了您的示例输入,将其另存为 in.csv,然后运行您的第一行,

awk -F"," '{print }' in.csv > out.txt

and it worked perfectly, like so:

它工作得很好,就像这样:

$ emacs in.csv
$ cat in.csv 
EXAMPLEfoo,60,6
EXAMPLEbar,30,6
EXAMPLE1,60,3
EXAMPLE2,120,6
EXAMPLE3,60,6
EXAMPLE4,30,6
$ awk -F"," '{print }' in.csv > out.txt
$ cat out.txt 
EXAMPLEfoo
EXAMPLEbar
EXAMPLE1
EXAMPLE2
EXAMPLE3

This is in Terminal.app on OS X 10.5

这是在 OS X 10.5 上的 Terminal.app 中

回答by Michal ?iha?

For me, cut produces expected result:

对我来说, cut 产生预期的结果:

cut -d, -f1 < in.csv > out.txt

回答by Chris Koknat

If Perl is an option:

如果 Perl 是一个选项:

perl -F, -lane 'print $F[0]' in.csv > out.txt

perl -F, -lane 'print $F[0]' in.csv > out.txt

These command-line options are used:

使用这些命令行选项:

  • -nloop around every line of the input file
  • -lremoves newlines before processing, and adds them back in afterwards
  • -aautosplit mode – split input lines into the @Farray. Defaults to splitting on whitespace.
  • -eexecute the perl code
  • -Fautosplit modifier, in this case splits on ,
  • -n循环输入文件的每一行
  • -l在处理之前删除换行符,然后将它们添加回
  • -a自动拆分模式 – 将输入行拆分到@F数组中。默认为在空白处拆分。
  • -e执行perl代码
  • -F自动拆分修饰符,在这种情况下拆分 ,

@Fis the array of words in each line, indexed starting with $F[0]

@F是每行中的单词数组,索引以 $F[0]