Search code examples
bashcut

Avoid Counting a Delimiter with Cut


I have a file that looks like this;

"id","string"
"10","john smith"
"20","smith, john"

On this I run cut -d , -f 2, which returns

"john smith"
"smith"

In the second row, I want to return "smith, john". Is there a solution to this using cut that ensures the specified delimiter is not treated as one when contained within a pair of double quotation marks?


Solution

  • awk -F'",' 'NR>1 {print $2}' file
    "john smith"
    "smith, john"
    

    In short: NR>1 skips first line and changing Field Separator by -F'",' one can print second field/column.