简体   繁体   中英

Using perl instead of grep

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?

PS: 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?).

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)'

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", $_;
    }
}

I think this is what you want

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

Hope it helps ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM