I am trying to use awk to cut with multi-character delimiter

echo 'f1##f2' |awk -F '##' '{print $2}'

It prints f2. Whereas

echo 'f1||f2' |awk -F '||' '{print $1}'

prints f1||f2.

Can anyone explain what is going on?

  • I'm surprised that GNU awk allows that at all. BSD awk throws an error, awk: illegal primary in regular expression || at |, as does mawk, mawk: line 0: regular expression compile failed (missing operand).
    – Kusalananda
    Sep 12, 2017 at 15:48
  • @Kusalananda, the behaviour is unspecified by POSIX. The GNU behaviour is more useful and makes more sense IMO. It makes more sense to me to have (foo|bar|) be like (foo|bar)? rather than returning an error. That's also what perl REs do. ? in zsh globs is done that (? meaning something different in globs) Sep 12, 2017 at 18:51

1 Answer 1


The vertical bar char | is treated as special character in your case and should be escaped:

echo "f1||f2" |awk -F'\\|\\|' '{print $1}'

As an alternative you may put | into the character class:

echo "f1||f2||f3" |awk -F'[|]{2}' '{print $3}'
  • Why escape it twice?
    – pfnuesel
    Sep 12, 2017 at 15:59
  • 1
    @pfnuesel, because with this -F'\\||' you'll get empty {print $2} (second field) Sep 12, 2017 at 16:02
  • 2
    @ctrl-alt-delor, FYI: linguistically | IS "vertical bar" char (not OR character), semantically - in some context it's logical OR operator, in some other context - regex alternation operator Sep 12, 2017 at 16:34
  • 1
    @ctrl-alt-delor Adding to the comment of RomanPerekhrest: Calling | an OR character is confusing because there is an actual OR character which is different: "∨" (Logical Or, U+2228, in Unicode block Mathematical Operators ). (In some fonts, it may look almost like the alphabet letter V in uppercase. It should be larger and have a larger angle.) Sep 13, 2017 at 1:27
  • @VolkerSiegel I was talking about its meaning in the language not the character name. In a regexp it is apparently called alternation, though I call it OR (not logical OR). Sep 13, 2017 at 8:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .