简体   繁体   English

使用Perl代替grep

[英]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中更改的所有文件的名称,并对它们执行svn add命令:

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 ". 我想:“我希望我可以使用Perl单行脚本而不是grep ”。

Is it possible to do and how if so? 有可能这样做吗?

PS: i found there is a m// operator in Perl. PS:我发现Perl中有一个m//运算符。 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?). 我认为应该在ARGV变量上使用它(在Perl中不知道它们的名称-可能是$_数组还是仅仅是$1类的变量?)。

Easy: 简单:

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

Or to do everything in Perl: 或在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. 可以使用perl单行代码,但是它仍然依赖于shell命令,除非您可以找到一个模块来处理svn调用。 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 ;) 希望能帮助到你 ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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