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

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


Your last option works perfectly for me:

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

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.csvin.unix.csv: ASCII text$ file in.dos.csvin.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,6EXAMPLEbar,30,6EXAMPLE1,60,3EXAMPLE2,120,6EXAMPLE3,60,6EXAMPLE4,30,6$ awk -F"," '{print $1}' in.csv > out.txt$ cat out.txt EXAMPLEfooEXAMPLEbarEXAMPLE1EXAMPLE2EXAMPLE3

This is in Terminal.app on OS X 10.5


For me, cut produces expected result:

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