简体   繁体   中英

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:

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". But I'm printing the second record. As shown on rubular :

在此处输入图片说明

  • How do I use a regex as the awk field separator?

Just replace \\d with [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:

$ 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 . So you'll need to add -e to enable escapes. Second of all awk doesn't support \\d so you have to use [0-9] or [[: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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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