简体   繁体   English

如何在AWK中将正则表达式用于字段分隔符?

[英]How to use a regex for the field separator in AWK?

I read another answer that show how one can set the field separator using the -F flag: 我读了另一个答案 ,显示了如何使用-F标志设置字段分隔符:

awk -F 'INFORMATION DATA ' '{print $2}' t

Now I'm curious how I can use a regex for the field separator. 现在,我很好奇如何将正则表达式用于字段分隔符。 My attempt can be seen below: 我的尝试可以在下面看到:

$ echo "1 2 foo\n2 3 bar\n42 2 baz"
1 2 foo
2 3 bar
42 2 baz
$ echo "1 2 foo\n2 3 bar\n42 2 baz" | awk -F '\d+ \d+ ' '{ print $2 }'
# 3 blank lines

I was expecting to get the following output: 我期望得到以下输出:

foo
bar
baz 

This is because my regex \\d+ \\d+ matches "the first 2 numbers separated by a space, followed by a space". 这是因为我的正则表达式\\d+ \\d+匹配“前两个数字,中间用空格隔开,后跟一个空格”。 But I'm printing the second record. 但是我正在打印第二条记录。 As shown on rubular : 如图所示上rubular

在此处输入图片说明

  • How do I use a regex as the awk field separator? 如何使用正则表达式作为awk字段分隔符?

Just replace \\d with [0-9] : 只需将\\d替换为[0-9]

With this you can print all the fields and you can see the fields immediatelly: 这样,您可以打印所有字段,并且可以立即看到这些字段:

$ echo -e "1 2 foo\n2 3 bar\n42 2 baz" |awk -v FS="[0-9]+ [0-9]+" '{for (k=1;k<=NF;k++) print k,$k}'
1 
2  foo
1 
2  bar
1 
2  baz

So just use [0-9] in your command: 因此,只需在命令中使用[0-9]:

$ echo -e "1 2 foo\n2 3 bar\n42 2 baz" |awk -v FS="[0-9]+ [0-9]+" '{print $2}'
 foo
 bar
 baz

First of all echo doesn't auto escape and outputs a literal \\n . 首先, echo不会自动转义并输出文字\\n So you'll need to add -e to enable escapes. 因此,您需要添加-e以启用转义。 Second of all awk doesn't support \\d so you have to use [0-9] or [[:digit:]] . 第二, awk不支持\\d因此您必须使用[0-9][[:digit:]]

echo -e "1 2 foo\n2 3 bar\n42 2 baz" | awk -F '[0-9]+ [0-9]+ ' '{ print $2 }'

or 要么

echo -e "1 2 foo\n2 3 bar\n42 2 baz" | awk -F '[[:digit:]]+ [[:digit:]]+ ' '{ print $2 }'

Both outputs: 两个输出:

foo
bar
baz 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM