I\'m trying different commands to process csv file where the separator is the pipe |
character.
While those commands do work when the comma is a separat
Try to escape the |
echo "more|data" | awk -F\| '{print $1}'
more
You tried "|"
, [|]
and /|
. /|
does not work because the escape character is \
, whereas []
is used to define a range of fields, for example [,-]
if you want FS
to be either ,
or -
.
To make it work "|"
is fine, are you sure you used it this way? Alternativelly, escape it --> \|
:
$ echo "he|llo|how are|you" | awk -F"|" '{print $1}'
he
$ echo "he|llo|how are|you" | awk -F\| '{print $1}'
he
$ echo "he|llo|how are|you" | awk 'BEGIN{FS="|"} {print $1}'
he
But then note that when you say:
print a[$2] [|] $4 [|] $5
so you are not using any delimiter at all. As you already defined OFS
, do:
print a[$2], $4, $5
Example:
$ cat a
he|llo|how are|you
$ awk 'BEGIN {FS=OFS="|"} {print $1, $3}' a
he|how are
You can escape the |
as \|
$ cat test
hello|world
$ awk -F\| '{print $1, $2}' test
hello world