2010-11-22 7 views
2

はグレップを使用して、次のコマンドライン出力を考えてみましょう:はgrepのためのPerlの交換を使用してファイル名を保持するために、どのように

[[email protected]]$ find . -name "*.php" | xargs grep __construct | tail 
./ilserverd/src/php/ImageLoopIntegrationService.php: public function __construct($input, $output=null) { 
./ilserverd/src/php/ImageLoopIntegrationService.php: public function __construct($vals=null) { 
./ilserverd/src/php/ImageLoopIntegrationService.php: public function __construct($vals=null) { 
./ilserverd/src/php/ImageLoopIntegrationService.php: public function __construct($vals=null) { 
./ilserverd/src/php/ImageLoopIntegrationService.php: public function __construct($vals=null) { 
./ilserverd/src/php/ImageLoopIntegrationService.php: public function __construct($handler) { 
./ilserverd/src/php/il_server_types.php: public function __construct($vals=null) { 
./ilserverd/src/php/il_server_types.php: public function __construct($vals=null) { 
./utilities/studio/legacy/full_bleed_update_photobook_themes.php: public function __construct() { 
./utilities/studio/legacy/full_bleed_update_photobook_themes.php:  parent::__construct(); 

これは、コンストラクタの引数を抽出しようとしているの私の最初のステップであり、そしてそれは、ステップ数が必要になりますので、 、私はgrepの改良としてPerlを使用しようとしています。しかし、まず、ファイル名を保持して、最終的な "レポート"出力のものを参照することができます。

しかし、次のPerlの1ライナーに切り替えると、ファイル名は出力に含まれなくなります。どうすればそれらを保持し、grepのコマンドラインの代わりにPerlを使用することができますか?

[[email protected]]$ find . -name "*.php" | xargs perl -wnl -e '/__construct/ and print' | tail 
    public function __construct($input, $output=null) { 
    public function __construct($vals=null) { 
    public function __construct($vals=null) { 
    public function __construct($vals=null) { 
    public function __construct($vals=null) { 
    public function __construct($handler) { 
    public function __construct($vals=null) { 
    public function __construct($vals=null) { 
    public function __construct() { 
     parent::__construct(); 

答えて

5

あなたはよう$ARGVを使用して試すことができます:Perlでのgrepのより良いバージョンが既に行われている建物

perl -wnl -e '/__construct/ and print "$ARGV: $_"' 
+2

http://search.cpan.org/perldoc?perlvar#ARGV – mob

3

。それはackと呼ばれています。

ack --type=php __construct 

または、各マッチライン(代わりの見出しなど)とファイル名したい場合:あなたはどちらか、find ... | xargs ...部分を必要としない

ack --type=php --nogroup __construct 

注意を。 --type=php引数がfindに代わるものです。

関連する問題