I have been having great success at using regex with capture groups with perl
, e.g.
for f in 123_abc_123.jpg 123_xyz_432.jpgdo echo $f echo $f | perl -ne 'if (/[0-9]+_([[a-z]+)_[0-9a-z]*/) { print $1 . "\n" }'done
Outputs:
123_abc_123.jpgabc123_xyz_432.jpgxyz
So the if-regex conditional in perl
will filter out all non-matching lines at the same time, for those lines that do match, it will apply the capture group(s) which you can access with $1
, $2
, ... respectively,