Return only matches from substitution in Perl 5.8.8 (was: Perl "p" equivalent to the regex modifier)

I have a script ( source ) to parse svn info in order to create a suitable string for Bash $PS1 . Unfortunately, this does not work on the same system I use Perl 5.8.8 on. It prints all lines instead of matches. What would Perl 5.8.8 be equivalent to the following?

 __svn_ps1() { local result=$( svn info 2>/dev/null | \ perl -pe 's;^URL: .*?/((trunk)|(branches|tags)/([^/]*)).*;\2\4 ;p') if [ -n "$result" ] then printf "${1:- (%s)}" $result fi } 

The result from Perl 5.10 contains only a space, brackets, a single branch name, a tag or trunk name, and an ending bracket. Exiting Perl 5.8.8 (without final p ) contains this plus version in brackets of each space-separated portion of svn info output.

A possible workaround involves a simple grep '^URL: ' between the svn and perl commands, but I was hoping to avoid this, as this will be done for every Bash hint.

+4
source share
2 answers

If you only want output from a string that matches, do not use the -p command line switch. It prints the value of $_ at the end of each loop. You might want something with the -n command line:

  perl -ne 'print if s/.../.../' 

I would do it the exact same way for Perl v5.8 and v5.10. I'm not sure what you think the /p modifier does, since you are not using the $` , $& or $' variables or their matching equivalents.

You can read command line options in perlrun .

+11
source

Starting with perl 5.10, the /p switch tells perl to put consistent content in ${^PREMATCH} , ${^MATCH} and ${^POSTMATCH} .

And the one liner you placed never uses these vars, so omit /p .

UPDATE: Trying to keep up with the initial question ...

 perl -ne 's/search/replace/ and print' 

Only lines for which a replacement has been made will be printed. Note -n compared to -p . Also, I tried the -p /p command on my 5.10, and it happily prints unchanged lines that don't match each other. Maybe I missed something ...

+6
source

Source: https://habr.com/ru/post/1332084/


All Articles