我该如何使用Perl制作一个类似于简单grep的批处理文件?

10

我已经知道了这个问题的显而易见的答案: "只需下载<插入您最喜欢的Windows grep或类似grep工具>。" 但是,我在一个受本地IT人员严格控制的环境中工作,他们控制着我们允许在计算机上安装什么。可以说:我有在Windows XP上访问Perl的权限。下面是一个我想出来的快速Perl脚本,它可以实现我想要的功能,但我还没有想出如何设置批处理文件,以便我可以将命令输出或传递文件(或文件列表?)作为“表达式”的参数之后:

perl -n -e "print $_ if (m![expression]!);" [filename]

我该如何编写批处理脚本,以便我可以做一些类似下面的事情:

dir | grep.bat mypattern
grep.bat mypattern myfile.txt

编辑:尽管我标记了另一个“答案”,但我想向Ray Hayes answer致敬,因为它确实是“Windows Way”来做到这一点,即使另一个答案在技术上更接近我想要的。


你想要使用grep的正则表达式语法还是更强大的perl语法? - Axeman
6个回答

27

大部分grep的功能已经在Windows应用程序FindStr.exe中提供,该程序是Windows 2000、XP和Vista机器的一部分! 它提供RegExpr等功能。

比批处理文件调用Perl要简单得多!

c:\>FindStr /?    
Searches for strings in files.

FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file]
        [/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]
        strings [[drive:][path]filename[ ...]]

  /B         Matches pattern if at the beginning of a line.
  /E         Matches pattern if at the end of a line.
  /L         Uses search strings literally.
  /R         Uses search strings as regular expressions.
  /S         Searches for matching files in the current directory and all
             subdirectories.
  /I         Specifies that the search is not to be case-sensitive.
  /X         Prints lines that match exactly.
  /V         Prints only lines that do not contain a match.
  /N         Prints the line number before each line that matches.
  /M         Prints only the filename if a file contains a match.
  /O         Prints character offset before each matching line.
  /P         Skip files with non-printable characters.
  /OFF[LINE] Do not skip files with offline attribute set.
  /A:attr    Specifies color attribute with two hex digits. See "color /?"
  /F:file    Reads file list from the specified file(/ stands for console).
  /C:string  Uses specified string as a literal search string.
  /G:file    Gets search strings from the specified file(/ stands for console).
  /D:dir     Search a semicolon delimited list of directories
  strings    Text to be searched for.
  [drive:][path]filename
             Specifies a file or files to search.

Use spaces to separate multiple search strings unless the argument is prefixed
with /C.  For example, 'FINDSTR "hello there" x.y' searches for "hello" or
"there" in file x.y.  'FINDSTR /C:"hello there" x.y' searches for
"hello there" in file x.y.

Regular expression quick reference:
  .        Wildcard: any character
  *        Repeat: zero or more occurances of previous character or class
  ^        Line position: beginning of line
  $        Line position: end of line
  [class]  Character class: any one character in set
  [^class] Inverse class: any one character not in set
  [x-y]    Range: any characters within the specified range
  \x       Escape: literal use of metacharacter x
  \<xyz    Word position: beginning of word
  xyz\>    Word position: end of word

这太棒了!对于一个试图在Windows平台上“站稳脚跟”的Unix极客来说,这是非常好的建议。即使它没有直接回答我的问题 :-) - Ogre Psalm33

12

下载并安装ack。它是grep的优秀替代品,由于Perl的魔术双模.BAT / Perl脚本技术支持,它将在命令行上为您工作。


好的,我特别指出我在电脑上有一个严格的环境,只允许我安装特定的软件。不过,这可能是一个很好的选择,因为它似乎是一个源代码下载,我的IT部门可能会让它通过。好建议。 - Ogre Psalm33
是的,我也想建議使用ack,但我知道某些IT部門甚至對未經授權的Firefox插件和Perl模塊也很嚴格。因此,如果你能自己編寫批處理文件,你可以這樣做。不過看起來FindStr比DOS find更強大。 - Axeman

5
我之前写过这个东西:
@rem = '--*-Perl-*--
@echo off
perl -x -S %0 %*
goto endofperl


@rem -- BEGIN PERL -- ';
#!d:/Perl/bin/perl.exe -w
#line 10
use strict; 
#use Test::Setup;
use Getopt::Long;

Getopt::Long::Configure ("bundling");

my $ignore_case    = 0;
my $number_line    = 0;
my $invert_results = 0;
my $verbose        = 0;

my $result = GetOptions( 
    'i|ignore_case' => \$ignore_case, 
    'n|number'      => \$number_line,
    'v|invert'      => \$invert_results,
    'verbose'       => \$verbose,
);
my $regex = shift;

if ( $ignore_case ) { 
    $regex = "(?i:$regex)";
}
$regex = qr/$regex/;
print "\$regex=$regex\n";
if ( $verbose ) { 
    print "Verbose: Ignoring case.\n"                      if $ignore_case;
    print "Verbose: Printing file name and line number.\n" if $number_line;
    print "Verbose: Inverting result set.\n"               if $invert_results;
    print "\n";
}

@ARGV = map { glob "$_" } @ARGV;

while ( <> ) { 
    my $matches = m/$regex/;
    next unless $matches ^ $invert_results;
    print "$ARGV\:$.:" if $number_line;
    print;
}

__END__
:endofperl

这是第一个“精确答案”响应,看起来正是我所要求的。但是,如前所述,“Windows Way”使用FINDSTR。然而,我一定会将这个小脚本保留在我的Windows Perl库中,并且我相信这个脚本或其某个变体在某些时候会派上用场! - Ogre Psalm33
实际上,我在我的电脑上有一个grep工具,但我想要使用Perl表达式进行搜索的能力。FindStr可能和grep一样好,但它也不会搜索文件。正如Dave Webb提到的那样,Ack两者都可以做到。 - Axeman

5

首先,将其转换为真正的脚本而不是一行代码:

use strict;
use warnings;

my $pattern = shift or die "Usage: $0 <pattern> [files|-]\n";
while (<>) { print if /$pattern/ }

然后使用pl2bat将其转换为批处理文件:
pl2bat mygrep.pl

这将创建“mygrep.bat”文件。
如果你只能运行Perl,那么完全使用Perl编写的功能齐全的grep(以及许多其他Unix应用程序)请参见Perl Power Tools项目。
虽然Perl Power Tools很好,但我通常更喜欢GnuWin32工具集。它们不需要安装。(你不需要管理员权限,只需要一个可以写入的目录。)

1
你需要做类似这样的事情:
@echo off
perl -x -S script.pl %1

"%1"将把参数传递给Perl脚本。将其保存为.bat文件,然后您就可以开始了。


这个能用管道符号吗?我的意思是如果我像这样使用你的批处理文件 gcc -MM files | yourbat regexp。我尝试过使用 ack,但得到了意外的结果。 - Gauthier

1

我同意Axeman和Hayes先生关于使用更好的工具来完成工作的观点。话虽如此,你可以尝试在批处理文件中使用以下代码来对文件通配符表达式运行自定义脚本:

@echo off

for /f "usebackq delims==" %%f in (`dir /w /b %2`) do (
    perl -n -e "print $_ if (m!%1!);" "%%f"
    REM or something like:  myperlscript.pl %1 "%%f"
)

这样,您可以执行类似于“grep mypattern myfile.txt”、“grep mypattern .”、“grep mypattern *.doc”等操作。


这是一个有用的小贴士。通过少量搜索,我在网络上找到了很少关于在批处理文件中使用参数、管道等的信息。这对我来说确实点亮了理解的灯泡,谢谢! - Ogre Psalm33

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接