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?