我已经知道这个问题的明显答案:“只需下载<插入最喜欢的窗口grep或类似grep的工具>”.但是,我在当地IT人员严格控制的环境中工作,我们允许在我们的电脑上.只需说:我可以在
Windows XP上访问Perl.这里有一个快速的Perl脚本,我想出了这个我想要的,但是我没有想到如何设置一个批处理文件,以便我可以管道命令输出到它,或传递一个文件(或文件列表?)作为“grep”表达式后的一个参数:
- perl -n -e "print $_ if (m![expression]!);" [filename]
如何编写一个批处理脚本,我可以做一些例如:
- dir | grep.bat mypattern
- grep.bat mypattern myfile.txt
编辑:即使我标注了另一个“答案”,我想给Ray Hayes answer作贡献,因为它真的是“Windows的方式”,即使另一个答案在技术上更接近我想要的.
我写了一段时间:
- @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