How to use grep to extract multiple groups
Let's say I have this file data.txt
:
a=0,b=3,c=5
a=2,b=0,c=4
a=3,b=6,c=7
I want to use grep
to retrieve 2 columns corresponding to values a
and c
:
0 5
2 4
3 7
I know how to fetch each column separately:
grep -oP 'a=\K([0-9]+)' data.txt
0
2
3
and
grep -oP 'c=\K([0-9]+)' data.txt
5
4
7
But I can't figure out how to extract the two groups. I tried the following, which didn't work:
grep -oP 'a=\K([0-9]+),.+c=\K([0-9]+)' data.txt
5
4
7
source to share
I'm also wondering what grep
might do this. \K
"removes" the previous content that is stored, so you cannot use it twice in the same expression: it will only display the last group. Therefore, it must be done differently.
At the same time, I would use sed
:
sed -r 's/^a=([0-9]+).*c=([0-9]+)$/\1 \2/' file
it catches digits after a=
and c=
when it does on lines starting with a=
and containing nothing after c=digits
.
To enter, enter:
0 5
2 4
3 7
source to share
You can try the grep command below. But note that grep displays each match on a separate new line. This way you won't get the format as you mentioned in the question.
$ grep -oP 'a=\K([0-9]+)|c=\K([0-9]+)' file
0
5
2
4
3
7
To get the specified format, you need to pipe the output grep
to paste
or any other commands.
$ grep -oP 'a=\K([0-9]+)|c=\K([0-9]+)' file | paste -d' ' - -
0 5
2 4
3 7
source to share