3

At work we use this construction to, for example, find out the names of all files, changed in SVN since last update and perform an svn add command on them:

svn st | grep '^\?' | perl -pe 's/^\s*\?// | xargs -L 1 svn add'

And i thought: "i wish i could use Perl one-line-script instead of grep".

Is it possible to do and how if so?

P.S.: i found there is a m// operator in Perl. I think it should be used on ARGV variables (do not know their names in Perl - may it be the $_ array or just $1 -like variables?).

2
  • 1
    What's wrong with grep? You could use grep -P if you need PCRE... Commented Dec 28, 2011 at 14:41
  • yeah, i could. but i wish i could use perl =) Commented Dec 28, 2011 at 14:44

3 Answers 3

4

Easy:

svn st | perl -lne 'print if s/^\s*\?//' | xargs -L 1 svn add

Or to do everything in Perl:

perl -e '(chomp, s/^\s*\?//) && system "svn", "add", $_ for qx(svn st)'
Sign up to request clarification or add additional context in comments.

1 Comment

expr for list means "assign the special global variable $_ in turn to each element of list, evaluating expr each time." See perldoc perlvar for more information on $_ and every other special Perl variable.
2

It's possible to use a perl one-liner, but it will still rely on shell commands, unless you can find a module to handle the svn calls. Not sure it will actually increase readability of performance, though.

perl -we 'for (qx(svn st)) { if (s/^\s*\?//) { system "svn", "add", $_ } }'

In a script version:

use strict;
use warnings;

for (qx(svn st)) {
    if (s/^\s*\?//) {
        system "svn", "add", $_;
    }
}

Comments

1

I think this is what you want

svn st | perl -ne 's/^\s*\?// && print' | xargs -L 1 svn add

Hope it helps ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.