Search code examples
linuxawkcut

Awk command to extract columns on dual delimiter


I have a file which contains rows like this

name[^legalName[^code[^type[^contactNumber1[^contactNumber2

I want to extract column two from this file. I don't find any problem when I have a single character delimiter. But how do I extract when there is a multiple character delimiter.


Solution

  • awk interprets the field separator as a regular expression, so you just need to double \\ escape each character to get the literals.

    echo 'name[^legalName[^code[^type[^contactNumber1[^contactNumber2' | awk -F'\\[\\^' '{print $2}'
    legalName