You are making the extremely common mistake of using square brackets for grouping. That's not what they do; they create a character class. You are looking for regular round parentheses (which apparently need to be backslash-quoted in your dialect of sed
):
sed -n 's/.*myProg v\(FOO \)\?\([[:alnum:][:punct:]]*\).*/v\2/p'
The initial /myProg v/
which you had, while well-intentioned, didn't really serve a useful purpose; the above script will only print lines where the substitution is successful, anyway.
(The /g
flag you had were simply not useful, as there would only ever be a single match on a line of input.)
This is assuming your sed
accepts \?
to mean "zero or one repetition". If it doesn't, you can somewhat inexactly but safely trade a *
wildcard instead.
grep -Po
. sed was the better option I found. – Maxim_united=~
-- I did two operations just out of laziness (to unify the cases before invoking the regex, and thus reduce the complexity / testing requirements for same). "As pipe manipulation" -- well, if that fits for what you're doing; just be aware that it has a performance cost. – Charles Duffy