Professional Web Applications Themes

How to reinvent grep with perl? - PERL Beginners

My man pages and info pages are not working well and I cannot figure out how to make grep search for a certain pattern. I even tried egrep and fgrep. So how do I reinvent grep with perl? Here is my attempt: perl -n -e 'print "$. $_" if /^ *END *$/' *.f This works better than grep, except for the fact it does not print the file name. How can I make perl print the file file name? How do I do this with (f\|e\|)grep? - woops - that is off topic. Never mind then. Just, how do I ...

  1. #1

    Default How to reinvent grep with perl?

    My man pages and info pages are not working well and I cannot figure out how
    to make grep search for a certain pattern. I even tried egrep and fgrep. So
    how do I reinvent grep with perl? Here is my attempt:



    perl -n -e 'print "$. $_" if /^ *END *$/' *.f



    This works better than grep, except for the fact it does not print the file
    name. How can I make perl print the file file name?



    How do I do this with (f\|e\|)grep? - woops - that is off topic. Never mind
    then. Just, how do I print the file name using perl?



    Thanks,

    Siegfried


    Siegfried Guest

  2. #2

    Default Re: How to reinvent grep with perl?

    Siegfried Heintze wrote: 

    There's no need. When you do 'man whatever', you can hit '/', type a search
    term, and hit enter. To search that same term again, '/' then enter will do.
    This works on my Gentoo Linux box.

    --
    Andrew Gaffney
    Network Administrator
    Skyline Aeronautics, LLC.
    636-357-1548

    Andrew Guest

  3. #3

    Default Re: How to reinvent grep with perl?

    Siegfried Heintze wrote: 

    If your 'man' uses 'less' like mine does, you hit Space to go to the next page
    and use the arrow keys to scroll one line at a time. I haven't used 'info' in a
    while, but I believe you can search those with 's'.

    --
    Andrew Gaffney
    Network Administrator
    Skyline Aeronautics, LLC.
    636-357-1548

    Andrew Guest

  4. #4

    Default Re: How to reinvent grep with perl?

    "Siegfried Heintze" <com> writes:
     

    grep -n '^ *END *$' *.f

    Harry Guest

  5. #5

    Default Re: How to reinvent grep with perl?

    "Siegfried Heintze" <com> writes:
     

    How is it better than grep?

    Harry Guest

  6. #6

    Default RE: How to reinvent grep with perl?

    Perl works better than grep because the grep statement you give below does
    not find any instances of the pattern and perl finds quite a few. I'm using
    Cygwin on Win2003Server. Since there is something obviously wrong with my
    cygwin implementation of grep, how do I get the file names with perl?

    This, works, but it sure is ugly. Is there not an easier way to do this with
    perl?

    perl -e'ARGV = ("-") unless ARGV; while(ARGV){ $ARGV= shift ARGV;
    if(!open(ARGV, $ARGV)){ warn "Cannot open $ARGV: $!\n"; next;} while
    (<ARGV>){ print "$ARGV:$.:$_\n" if/^ *END *$/; }}' *.f

    Thanks,
    Siegfried

    -----Original Message-----
    From: news [mailto:gmane.org] On Behalf Of Harry Putnam
    Sent: Saturday, October 09, 2004 4:33 PM
    To: org
    Subject: Re: How to reinvent grep with perl?

    "Siegfried Heintze" <com> writes:
     
    how 
    So 

    grep -n '^ *END *$' *.f


    --
    To unsubscribe, e-mail: org
    For additional commands, e-mail: org
    <http://learn.perl.org/> <http://learn.perl.org/first-response>



    Siegfried Guest

  7. #7

    Default Re: How to reinvent grep with perl?

    "Siegfried Heintze" <com> writes:
     

    Doesn't something simple like:

    perl -n -e 'print "$ARGV\n$. $_" if /PATTERN/' *.f
    work for you.

    Harry Guest

  8. #8

    Default Re: How to reinvent grep with perl?

    Harry Putnam wrote:
     

    There ist one problem if you are running this command with several files
    at once. The line number $. will not be set to zero when the next file
    is opened. This will mimic grep's behaviour better:

    perl -lne 'close ARGV if eof;print "$ARGV:$.:$_" if /PATTERN/' *.f

    But for better usability it probably better placed in a file as a script
    of it's own. (like perlgrep ??) The only tricky thing about this is to
    get the regexp from the command line (qr// operator) and to find a nice
    way to handle the regexp-modifiers. (or simply use "(?imsx)" )

    resources:
    -- perldoc perlop (section about $.)
    -- perldoc -f eof
    (nice information about one-liners at IBM developerWorks)
    -- http://www-106.ibm.com/developerworks/linux/library/l-p101/
    -- http://www-106.ibm.com/developerworks/linux/library/l-p102.html
    Henner Guest

Similar Threads

  1. grep in perl
    By mk76@buffalo.edu in forum PERL Beginners
    Replies: 2
    Last Post: July 16th, 12:26 PM
  2. using grep in perl
    By Andrew Gaffney in forum PERL Beginners
    Replies: 3
    Last Post: February 3rd, 05:03 PM
  3. perl grep..
    By John W. Krahn in forum PERL Beginners
    Replies: 0
    Last Post: January 16th, 11:30 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139