Using pipe character as a field separator

馋奶兔 提交于 2020-07-14 12:47:11

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!