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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityThe vertical bar char |
is treated as special character in your case and should be escaped:
echo "f1||f2" |awk -F'\\|\\|' '{print $1}'
f1
As an alternative you may put |
into the character class:
echo "f1||f2||f3" |awk -F'[|]{2}' '{print $3}'
f3
-F'\\||'
you'll get empty {print $2}
(second field)
Sep 12, 2017 at 16:02
|
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
|
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
awk
allows that at all. BSDawk
throws an error,awk: illegal primary in regular expression || at |
, as doesmawk
,mawk: line 0: regular expression compile failed (missing operand)
.(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)