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

*爱你&永不变心* 提交于 2019-11-28 21:09:16

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.

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

For me, cut produces expected result:

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

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]

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!