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

发布于 2024-08-28 23:57:24 字数 644 浏览 6 评论 0原文

操作系统: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情话难免假 2024-09-04 23:57:24

你的最后一个选择非常适合我:

$ 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

也许行结尾在这里咬你?如果文件具有 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

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

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

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.csv and see what it comes up with.

$ 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 dos2unix tool to convert the file.

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

暗恋未遂 2024-09-04 23:57:24

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

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

它运行得很好,如下所示:

$ 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 $1}' in.csv > out.txt
$ cat out.txt 
EXAMPLEfoo
EXAMPLEbar
EXAMPLE1
EXAMPLE2
EXAMPLE3

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,

awk -F"," '{print $1}' 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 $1}' in.csv > out.txt
$ cat out.txt 
EXAMPLEfoo
EXAMPLEbar
EXAMPLE1
EXAMPLE2
EXAMPLE3

This is in Terminal.app on OS X 10.5

紫﹏色ふ单纯 2024-09-04 23:57:24

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

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

For me, cut produces expected result:

cut -d, -f1 < in.csv > out.txt
三寸金莲 2024-09-04 23:57:24

如果 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]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文