将 csv 文件的第一列解析为新文件
操作系统:OSX 方法:从命令行,使用 sed、cut、gawk,尽管最好不安装模块。
本质上,我试图获取 csv 文件的第一列并将其解析为一个新文件。
输入文件示例
EXAMPLEfoo,60,6
EXAMPLEbar,30,6
EXAMPLE1,60,3
EXAMPLE2,120,6
EXAMPLE3,60,6
EXAMPLE4,30,6
期望输出
EXAMPLEfoo
EXAMPLEbar
EXAMPLE1
EXAMPLE2
EXAMPLE3
EXAMPLE4
所以我想要第一列。
这是我到目前为止所尝试过的:
awk -F"," '{print $1}' in.csv > out.txt
awk -F"," '{for (i=2;i<=NF;i++)}' in.csv > out.txt
awk -F"," 'BEGIN { OFS="," }' '{print $1}' in.csv > out.txt
cat in.csv | cut -d \, -f 1 > out.txt
似乎都不起作用,要么只打印第一行,要么根本不打印任何内容,所以我认为它无法逐行读取。
Operating System: OSX
Method: From the command line, so using sed, cut, gawk, although preferably no installing modules.
Essentially I am trying to take the first column of a csv file and parse it to a new file.
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 $1}' in.csv > out.txt
awk -F"," '{for (i=2;i<=NF;i++)}' in.csv > out.txt
awk -F"," 'BEGIN { OFS="," }' '{print $1}' 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的最后一个选择非常适合我:
也许行结尾在这里咬你?如果文件具有 DOS 风格甚至旧 Mac 风格的行结尾,这可能会导致奇怪的行为。尝试运行
file in.csv
并查看结果。如果您的情况是后者,请使用
dos2unix
工具转换文件。编辑:在 OS X 上,似乎
flip 就是你想要的
。
Your last option works perfectly for me:
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.csv
and see what it comes up with.If the latter is your situation, use the
dos2unix
tool to convert the file.Edit: On OS X, it seems
flip
is what you want.我复制粘贴了您的示例输入,将其另存为 in.csv,然后运行您的第一行,
它运行得很好,如下所示:
This is in Terminal.app on OS X 10.5
I copy-pasted your sample input, saved it as in.csv, and then ran your first line,
and it worked perfectly, like so:
This is in Terminal.app on OS X 10.5
对我来说,cut 产生了预期的结果:
For me, cut produces expected result:
如果 Perl 是一个选项:
perl -F, -lane 'print $F[0]' in.csv > out.txt
使用这些命令行选项:
-n
循环输入文件的每一行-l
在处理之前删除换行符,并将其添加回来之后的-a
自动分割模式 - 将输入行分割到@F
数组中。默认为按空格分割。-e
执行perl代码-F
自动分割修饰符,在本例中分割,
@F
是数组每行中的单词数,以$F[0]
开头索引If Perl is an option:
perl -F, -lane 'print $F[0]' in.csv > out.txt
These command-line options are used:
-n
loop around every line of the input file-l
removes newlines before processing, and adds them back in afterwards-a
autosplit mode – split input lines into the@F
array. Defaults to splitting on whitespace.-e
execute the perl code-F
autosplit modifier, in this case splits on,
@F
is the array of words in each line, indexed starting with$F[0]