How to get the second column from command output?

Qiang Xu picture Qiang Xu · Apr 22, 2013 · Viewed 348.7k times · Source

My command's output is something like:

1540 "A B"
   6 "C"
 119 "D"

The first column is always a number, followed by a space, then a double-quoted string.

My purpose is to get the second column only, like:

"A B"
"C"
"D"

I intended to use <some_command> | awk '{print $2}' to accomplish this. But the question is, some values in the second column contain space(s), which happens to be the default delimiter for awk to separate the fields. Therefore, the output is messed up:

"A
"C"
"D"

How do I get the second column's value (with paired quotes) cleanly?

Answer

Alex picture Alex · Apr 22, 2013

Use -F [field separator] to split the lines on "s:

awk -F '"' '{print $2}' your_input_file

or for input from pipe

<some_command> | awk -F '"' '{print $2}'

output:

A B
C
D