Issue
I have a statement that looks something like this:
DO prog3 WHILE prog1 arg1 arg2 <= prog2 arg1 END
I would like to extract the parameters prog3
, prog1 arg1 arg2
, <=
(which could be any operator), prog2 arg1
from the statement, using grep-style regex.
My command is:
grep -E 'DO(.*)WHILE(.*)[<=](.*)END' <<< \
'DO prog3 WHILE prog1 arg1 arg2 <= prog2 arg1 END' -o
The regex works on regex101.com, but not in grep, which simply returns the whole statement as a match, ie
DO prog3 WHILE prog1 arg1 arg2 <= prog2 arg1 END
How can I fix this?
Solution
grep
doesn't output all capture groups. It would be better to use sed
like this:
s='DO prog3 WHILE prog1 arg1 arg2 <= prog2 arg1 END'
sed -E 's/DO (.*) WHILE (.*) ([<>=]+) (.*) END/\1\n\2\n\3\n\4/' <<< "$s"
prog3
prog1 arg1 arg2
<=
prog2 arg1
Here:
- Used
[<>=]+
in a separate capture group to grab operator text - Used spaces around capture groups to handle greediness of
.*
- Use
\n
after each back reference to print each group on a separate line likegrep -o
Answered By - anubhava Answer Checked By - Dawn Plyler (WPSolving Volunteer)