Windows – 如何使批处理文件像使用Perl的简单grep一样行事?

前端之家收集整理的这篇文章主要介绍了Windows – 如何使批处理文件像使用Perl的简单grep一样行事?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经知道这个问题的明显答案:“只需下载<插入最喜欢的窗口grep或类似grep的工具>”.但是,我在当地IT人员严格控制的环境中工作,我们允许在我们的电脑上.只需说:我可以在 Windows XP上访问Perl.这里有一个快速的Perl脚本,我想出了这个我想要的,但是我没有想到如何设置一个批处理文件,以便我可以管道命令输出到它,或传递一个文件(或文件列表?)作为“grep”表达式后的一个参数:
  1. perl -n -e "print $_ if (m![expression]!);" [filename]

如何编写一个批处理脚本,我可以做一些例如:

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

编辑:即使我标注了另一个“答案”,我想给Ray Hayes answer作贡献,因为它真的是“Windows的方式”,即使另一个答案在技术上更接近我想要的.

我写了一段时间:
  1. @rem = '--*-Perl-*--
  2. @echo off
  3. perl -x -S %0 %*
  4. goto endofperl
  5.  
  6.  
  7. @rem -- BEGIN PERL -- ';
  8. #!d:/Perl/bin/perl.exe -w
  9. #line 10
  10. use strict;
  11. #use Test::Setup;
  12. use Getopt::Long;
  13.  
  14. Getopt::Long::Configure ("bundling");
  15.  
  16. my $ignore_case = 0;
  17. my $number_line = 0;
  18. my $invert_results = 0;
  19. my $verbose = 0;
  20.  
  21. my $result = GetOptions(
  22. 'i|ignore_case' => \$ignore_case,'n|number' => \$number_line,'v|invert' => \$invert_results,'verbose' => \$verbose,);
  23. my $regex = shift;
  24.  
  25. if ( $ignore_case ) {
  26. $regex = "(?i:$regex)";
  27. }
  28. $regex = qr/$regex/;
  29. print "\$regex=$regex\n";
  30. if ( $verbose ) {
  31. print "Verbose: Ignoring case.\n" if $ignore_case;
  32. print "Verbose: Printing file name and line number.\n" if $number_line;
  33. print "Verbose: Inverting result set.\n" if $invert_results;
  34. print "\n";
  35. }
  36.  
  37. @ARGV = map { glob "$_" } @ARGV;
  38.  
  39. while ( <> ) {
  40. my $matches = m/$regex/;
  41. next unless $matches ^ $invert_results;
  42. print "$ARGV\:$.:" if $number_line;
  43. print;
  44. }
  45.  
  46. __END__
  47. :endofperl

猜你在找的Windows相关文章