Parsing 解析awk中以管道分隔的输入

Parsing 解析awk中以管道分隔的输入,parsing,awk,pipe,delimited,Parsing,Awk,Pipe,Delimited,我见过很多帖子问类似的问题。不能让它工作 输入看起来像: <field one with spaces>|<field two with spaces> 仍然无法使管道分隔符工作 使用CentOS 有什么帮助吗 echo "field one has spaces | field two has spaces" \ | awk ' BEGIN { FS="|" } { print $2 print $1 # or what

我见过很多帖子问类似的问题。不能让它工作

输入看起来像:

<field one with spaces>|<field two with spaces>
仍然无法使管道分隔符工作

使用CentOS

有什么帮助吗

 echo "field one has spaces | field two has spaces" \
 | awk '
   BEGIN {
      FS="|" 
 }
 {
   print $2
   print $1
   # or what ever you want
 }'

 #output

  field two has spaces
  field one has spaces
您还可以将其简化为

awk -F'|' {
    print $2
    print $1
}'
编辑 此外,并非所有AWK都可以将多字符正则表达式用作
FS

Edit2 不知何故,我最初没有注意到这一点,但我看到您试图在
|
字符的字符类pre和post中包含
\x00
。我想你是指
\x00
=
null
char?我认为您不能让
awk
解析嵌入了空字符的文件。你可以像这样处理你的输入

 tr '\x00'   ' ' < file.txt > spacesForNulls.txt 
tr'\x00'spacesForNulls.txt
或连同

tr -d '\x00' < file.txt > deletedNulls.txt
tr-d'\x00'deletedNulls.txt
去掉正则表达式中的那部分。但是如上所述,一些
awk
不支持
FS
值的正则表达式。而且,我不经常使用
tr
技巧,您可能会发现它需要对
null
字符使用稍微不同的符号,这取决于您的
tr
版本


我希望这能有所帮助。

这是
\x00
的一大亮点。或者op应该使用更专门的工具,如
perl
ruby
++
我认为您不能让awk解析一个嵌入空字符的文件
或者再想想<代码>awk'{gsub(“\x00,”)}1是可能的。
tr -d '\x00' < file.txt > deletedNulls.txt