0

I have the regex[1]

(/foo/bar/(GW|GREV)[^/]*).*

I was able to get this to work with GNU sed with

sed -n 's/\(\/foo\/bar\/\(GW\|GREV\)[^\/]*\).*/\1/p'

But that was a modern GNU sed and I'm on Solaris 10 with an ancient sed. Maybe it is possible to port it but that's beyond my ability. I do have perl 5.8 available to me, though, so my question: how can I get the matching subgroup printed in a perl one liner?


[1] The gist of it is I want to match all directories that match the form /foo/bar/GWxxx, /foo/bar/GREVxxx and print that root directory. So for the following strings, I always get /foo/bar/GREV123 as the captured group:

/foo/bar/GREV123
/foo/bar/GREV123/
/foo/bar/GREV123/etc
  • Have you tried grep? gnu.org/software/grep/manual/grep.html Would be easy enough to get a listing then pipe it into grep. If /foo/bar/GREV123 is what you are after then you can limit your expression in grep to only give you that in the return. – VenomFangs Sep 22 '15 at 21:28
  • I can try grep, but once again i just have grep/egrep from solaris 10, not the fully-featured GNU grep – ldrg Sep 22 '15 at 23:29
2
perl -pe 's{^(/foo/bar/(GW|GREV)[^/]*).*?$}{$1}o'

Deparsed version:

LINE: while (defined($_ = <ARGV>)) {
    s[^(/foo/bar/(GW|GREV)[^/]*).*?$][$1]o;
}
continue {
   die "-p destination: $!\n" unless print $_;
}

P.S. You may add code to supress printing duplicated entries

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.