Search code examples
linuxunixpipedelimiterseparator

Pipe is not recognised when in a multi-character delimiter


I have a file delimited with |;|. Example;

1|;|2|;|3|;|4|;|5
1|;|2|;|3|;|4|;|5

I need to change the delimiter to : or something else,

but FS in awk does not do anything to pipes |

awk '$1=$1' FS="|;|" OFS=":" file

1|:|2|:|3|:|4|:|5
1|:|2|:|3|:|4|:|5 

pipe | is recognized when used as a single character separator;

awk '$1=$1' FS="|" OFS="" file

1:2:3:4:5
1:2:3:4:5 

however, sed resolves the issue,

sed 's/|;|/:/g' file

but I am wondering if there is a way to do with awk command.


Solution

  • FS contains a regular expression, | mean alternative in regular expressions. Backslash the pipes to match them literally:

    awk '$1=$1' FS='\\|;\\|' OFS=:
    

    The reason while a single pipe works is one-character FS is treated specially:

    If FS is a single character, fields are separated by that character. If FS is the null string, then each individual character becomes a separate field. Otherwise, FS is expected to be a full regular expression. In the special case that FS is a single space, fields are separated by runs of spaces and/or tabs and/or newlines.