我已经知道这个问题的明显答案:<!>“只需下载<!> lt;在这里插入喜爱的windows grep或grep-like工具<!> gt; <!> quot;。但是,我在当地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回答,因为它真的是<!> ; Windows Way <!> quot;要做到这一点,即使另一个答案在技术上更接近我想要的。

有帮助吗?

解决方案

我写了一会儿:

@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

其他提示

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

下载并安装确认。它是grep的优秀替代品 - 感谢Perl的神奇双模式.BAT / Perl脚本魔法 - 它可以在命令行上运行。

首先,将其变成真实的剧本而不是单行:

use strict;
use warnings;

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

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

pl2bat mygrep.pl

这将创建<!> quot; mygrep.bat <!>“。

对于完全用Perl编写的功能齐全的grep(以及许多其他Unix应用程序),请参阅 Perl Power Tools 项目。

如果您只能运行Perl,Perl Power Tools是好的,我通常更喜欢 GnuWin32 工具。它们不需要安装。 (您不需要管理权限,只需要您可以写入的目录。)

我同意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 <!>“;等等。

你需要做这样的事情:

@echo off
perl -x -S script.pl %1

<!>“;%1 <!>”;将参数传递给Perl脚本。将它保存为.bat文件,你很高兴。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top