Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

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

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

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


$ echo 'foo-|-bar' | awk 'BEGIN {FS="-\|-"} {print $2}'
awk: warning: escape sequence `\|' treated as plain `|'
share|improve this question
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
up vote 6 down vote accepted

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}'


echo 'foo-|-bar' | awk -F '-\\|-' '{print $2}'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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