The key point here is that the channel symbol ( | ) must be escaped into the shell. Use ' \| "or' '|' "to protect it from shell interference and allow it to be passed to awk on the command line.
Looking at the comments, I see that the original poster is a simplified version of the original problem, which included filtering the file before selecting and printing the fields. A pass through grep , and the result was passed to awk to select the field. This explains the completely unnecessary cat file that appears in the question (it replaces grep <pattern> file ).
Ok, that will work. However, awk is basically a pattern matching tool, and you can trust it to find and work with the appropriate lines without having to call grep . Use something like:
awk -F\| '/<pattern>/{print $2;}{next;}' file
The bit /<pattern>/ tells awk to perform an action that follows the lines that correspond to <pattern> .
Lost {next;} is the default action, which moves to the next line in the input. It does not seem necessary, but I have this habit a long time ago ...
dmckee
source share