2

This is the expected way for the awk field separator to work:

$ echo 'fooXbar' | awk 'BEGIN {FS="X"} {print $1}'
foo
$ echo 'fooXbar' | awk 'BEGIN {FS="X"} {print $2}'
bar
$ 

but if the FS is "-|-" then things get strange:

$ echo 'foo-|-bar' | awk 'BEGIN {FS="-|-"} {print $1}'
foo
$ echo 'foo-|-bar' | awk 'BEGIN {FS="-|-"} {print $2}'
|
$ echo 'foo-|-bar' | awk 'BEGIN {FS="-|-"} {print $3}'
bar
$ 

Why? Why is the $2 a "|" in the second example??

UPDATE:

$ echo 'foo-|-bar' | awk 'BEGIN {FS="-\|-"} {print $2}'
awk: warning: escape sequence `\|' treated as plain `|'
|
$ 
  • 2
    Multi-character field separators are treated as regular expressions in awk. You should always escape them appropriately if you want the literal meaning. – Chris Down Feb 23 '14 at 13:29
7

FS is a regex: awk interprets -|- as "- or -".

Use -\|- instead. You'll need to double the backslash inside the string literal.

echo 'foo-|-bar' | awk 'BEGIN {FS="-\\|-"} {print $1}'

or

echo 'foo-|-bar' | awk -F '-\\|-' '{print $2}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.