I've see a bunch of examples, but I just can't seem to make this work. Can grep output only specified groupings that match? for example seems like it should work, but I get either errors or no output at all.
I want to do:
pathname="/a/long/path/of/mine/2x02 - bar.mp4"
All of the examples will be the long path, one or two digits, x and then 2 digits followed by a space, a - and a filename.
I want to parse this for the 02 value: https://regex101.com/ shows that \d{1,2}x(\d\d) should have match 1 = 02 in this example.
What I can't figure out is if I have
echo "$pathname" | sed -n 's/.*\d{1,2}x\(\d\d\)/\1/p'
or
echo $pathname | grep -oP '\d{1,2}x(\d\d)'
I get nothing. I can do:
echo $pathname | grep -oP '(\d\d)'
but there could be cases where there are other 2 digit values in a row, like if I had
/a/long/path/of/mine/12x02 - bar.mp4
in which case I don't think the above will specify the second match, so I prefer the more specific regex... IF I can use matching groups or something. I'm trying to do this in bash on Scientific Linux 7.1.
egrep
(orgrep -E
, which is the same) to enable some of the regexp functionality.-E
) regular expressions have "no difference in available functionality". Also, the OP is already using something more powerful than-E
. He is usinggrep -P
which means that his grep supports perl-style regexes.-P
, thanks!