How do I print a field from a pipe-separated file?

Jon Ericson picture Jon Ericson · Oct 17, 2008 · Viewed 27.4k times · Source

I have a file with fields separated by pipe characters and I want to print only the second field. This attempt fails:

$ cat file | awk -F| '{print $2}'
awk: syntax error near line 1
awk: bailing out near line 1
bash: {print $2}: command not found

Is there a way to do this?

Answer

Zsolt Botykai picture Zsolt Botykai · Oct 17, 2008

Or just use one command:

cut -d '|' -f FIELDNUMBER