Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: How to print the lines immediately above and below a matching line?

by afoken (Chancellor)
on Nov 25, 2012 at 16:56 UTC ( [id://1005508]=note: print w/replies, xml ) Need Help??


in reply to How to print the lines immediately above and below a matching line?

/tmp>grep -C1 halt /etc/passwd shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/:/bin/false /tmp>ack -C1 halt /etc/passwd shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/:/bin/false /tmp>

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^2: How to print the lines immediately above and below a matching line?
by karlgoethebier (Abbot) on Nov 25, 2012 at 21:54 UTC

    Yes...

    Karls-Mac-mini:Desktop karl$ grep -C1 4003 MyData.txt 4002_1#2 4003_1#3 4004_1#4

    ...but do you really want to qx this on a 4 GByte file? Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      but do you really want to qx this on a 4 GByte file?

      No. I would not use Perl at all just to call grep. My shell can start grep fine without needing Perl.

      4 GByte should be no problem for grep, at least not for GNU grep. Actually, I expect grep to be at least as fast as a perl script, and I expect it to use less memory. Simply because grep is optimized for exactly that job.

      By the way: grep has lots of other useful options, like showing line numbers and/or file names, again no need to write Perl code.

      A quite useful alternative to grep is ack. It shares many features with GNU grep, and does some things better. ack ignores files and directories you typically do not want to search by default, it uses Perl regexp syntax instead of "basic" or "extended" regexp syntax, and it has a configuration file.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        OK, but what if you need to process the result? Do you pipe it to some perl? What if no GNU grep on board? I'm interested in this question because i have to do some similar tasks in a monitoring context (using nagios). So qxing/piping matters. Thank you for your answer and best regards, Karl

        «The Crux of the Biscuit is the Apostrophe»

Re^2: How to print the lines immediately above and below a matching line?
by Anonymous Monk on Nov 25, 2012 at 18:30 UTC
    shh! don't give away the secrets

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1005508]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-03-29 09:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found