كيف يمكنني إنشاء ملف دفعي ليعمل مثل grep بسيط باستخدام Perl؟

StackOverflow https://stackoverflow.com/questions/106053

  •  01-07-2019
  •  | 
  •  

سؤال

أعرف بالفعل الإجابة الواضحة على هذا السؤال:"فقط قم بتنزيل <أدخل أداة grep المفضلة أو أداة شبيهة بـ grep هنا>".ومع ذلك، فأنا أعمل في بيئة تخضع لضوابط صارمة من قبل موظفي تكنولوجيا المعلومات المحليين فيما يتعلق بما يُسمح لنا بوجوده على أجهزة الكمبيوتر الخاصة بنا.ويكفي ان نقول:لدي إمكانية الوصول إلى Perl على نظام التشغيل Windows XP.إليك نص Perl السريع الذي توصلت إليه والذي يفعل ما أريد، لكنني لم أكتشف كيفية إعداد ملف دفعي بحيث يمكنني إما توجيه إخراج الأمر إليه، أو تمرير ملف (أو قائمة الملفات) ?) كوسيطة بعد "تعبير grep":

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

كيف أكتب برنامج نصي دفعي يمكنني أن أفعل شيئًا مثل، على سبيل المثال:

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

يحرر:على الرغم من أنني قمت بوضع علامة على "إجابة" أخرى، إلا أنني أردت أن أشيد بها الإجابة راي هايز, ، لأنها حقًا "طريقة 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

نصائح أخرى

معظم قوة 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

تحميل وتثبيت ack.إنه بديل رائع لـ grep و- بفضل الوضع المزدوج السحري لـ Perl .BAT / Perl script magic - سيعمل على سطر الأوامر نيابةً عنك.

أولاً، قم بتحويله إلى نص حقيقي بدلاً من مجرد سطر واحد:

use strict;
use warnings;

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

ثم قم بتحويله إلى ملف دفعي باستخدام pl2bat:

pl2bat mygrep.pl

سيؤدي هذا إلى إنشاء "mygrep.bat".

للحصول على grep كامل المواصفات (والعديد من تطبيقات Unix الأخرى) مكتوب بالكامل بلغة Perl، راجع ملف أدوات بيرل الكهربائية مشروع.

على الرغم من أن أدوات Perl Power Tools جيدة إذا كان بإمكانك تشغيل Perl فقط، إلا أنني أفضل بشكل عام مجموعة جنووين32 أدوات.أنها لا تتطلب التثبيت.(لا تحتاج إلى امتيازات إدارية، فقط دليل يمكنك الكتابة إليه.)

وأنا أتفق مع أكسمان والسيد.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