问题
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 separator, it throws an error when I replace it with the pipe:
awk -F[|] "NR==FNR{a[$2]=$0;next}$2 in a{ print a[$2] [|] $4 [|] $5 }" OFS=[|] file1.csv file2.csv
awk "{print NR "|" $0}" file1.csv
I tried, "|"
, [|]
, /|
to no avail.
I'm using Gawk on windows. What I'm I missing?
回答1:
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
回答2:
Try to escape the |
echo "more|data" | awk -F\| '{print $1}'
more
回答3:
You can escape the |
as \|
$ cat test
hello|world
$ awk -F\| '{print $1, $2}' test
hello world
来源:https://stackoverflow.com/questions/27246584/using-pipe-character-as-a-field-separator