grep multiple lines

August 9th, 2007 mysurface Posted in grep, Text Manipulation | Hits: 122494 | 21 Comments »

You can grep multiple lines before or after matching the keywords. Here is a simple tips, that what I discover grep capable of. A is after, B is before.

Let say you have the message.txt shows as below:


Aug  5 02:43:12 zion kernel: [    0.000000] Zone PFN ranges:
Aug  5 02:43:12 zion kernel: [    0.000000]   DMA             0 ->     4096
Aug  5 02:43:12 zion kernel: [    0.000000]   Normal       4096 ->   130730
Aug  5 02:43:12 zion kernel: [    0.000000]   HighMem    130730 ->   130730
Aug  5 02:43:12 zion kernel: [    0.000000] early_node_map[1] active PFN ranges
Aug  5 02:43:12 zion kernel: [    0.000000]     0:        0 ->   130730
Aug  5 02:43:12 zion kernel: [    0.000000] DMI 2.3 present.

You can grep “DMA” with -B 1 and -A2

grep -B1 -A2 "DMA" message.txt

It will return

Aug  5 02:43:12 zion kernel: [    0.000000] Zone PFN ranges:
Aug  5 02:43:12 zion kernel: [    0.000000]   DMA             0 ->     4096
Aug  5 02:43:12 zion kernel: [    0.000000]   Normal       4096 ->   130730
Aug  5 02:43:12 zion kernel: [    0.000000]   HighMem    130730 ->   130730

It is useful to grep logs with multiple lines for one entry.

[中文翻译]

21 Responses to “grep multiple lines”

  1. This is damm cool. Never use this option before. Thanks for your tips.

  2. I use that feature all the time. I think grep & sed are my best friends. :)

  3. Could you please me how to do the same in HP-UX?
    Thank you in advance

  4. Sasikanth Malladi Says:

    To do the same on HP-UX, just install GNU grep on your machine.

  5. Shery Vijayan Says:

    Thanks pal , for the tip.
    That was extremely usefull.
    –Shery

  6. I have a slightly more complicated problem. Let’s assume I have a file like below

    sdfsdghf
    DMA
    DMA
    45346768
    sdfsdgfh
    DMA
    DMA
    DMA
    5236472646
    DMA
    *&*(&*(&

    Now I want to find out how many times DMA appear in consecutive lines. How can I do that? In above case I should get result as 2, 3 & 1

  7. Try using
    uniq -c | grep DMA

  8. That is an awesome tip. I was going to write an Perl script to extract stuff like this, but figure I should check Grep’s awesome utility first. Thanks!!

  9. grep -B1 -A2 “DMA” message.txt works very well, but what if I want to filter away all such matches?

    Normally the -v option will do, but it doesn’t seem to work with -B or -A options… Any clue?

  10. What if the my pattern is not fixed but i want get a block of data?
    For example in a big file i have this whole string
    ##
    # Host alias specification
    ##
    Host_Alias SPARC = bigtime, eclipse, moet, anchor:\
    SGI = grolsch, dandelion, black:\
    ALPHA = widget, thalamus, foobar:\
    HPPA = boa, nag, python
    Host_Alias CUNETS = 128.138.0.0/255.255.0.0
    Host_Alias CSNETS = 128.138.243.0, 128.138.204.0/24, 128.138.242.0
    Host_Alias SERVERS = master, mail, www, ns
    Host_Alias CDROM = orion, perseus, hercules

    ##

    Host_Alias SPARC = bigtime, eclipse, moet, anchor:\
    SGI = grolsch, dandelion, black:\
    ALPHA = widget, thalamus, foobar:\
    HPPA = boa, nag, python

    This is a single line comin

    I have smthng like this…
    how can get all lines starting with host alias?

  11. This is the file that I am reading. When I run grep looking for “Core0″ or “Core1″ I get two results back. How can I seperate the different returns? -b 1 will give me the last item but, I cannot find a way to get just the first item of the pair.

    8temp-pci-00c3
    Adapter: PCI adapter
    Core0 Temp: +44.0°C
    Core0 Temp: +41.0°C
    Core1 Temp: +37.0°C
    Core1 Temp: +41.0°C

  12. Arindam Paul Says:

    Excellent … Just excellent!! Thanks a lot

  13. Too good… I was breaking my head to find such an option!

  14. This is damm cool. Never knew such options existed in grep
    how long has this been there.

  15. Hi, what if I want to grep random lines eg. line 1,4,7 after a match?

  16. @Babu: Oh since about 1973…

  17. Thanks a lot. It was very useful.

  18. EXCELLENT !

  19. That’s freaking cool! Thank you so much for your help!

  20. Hi..i want to search search multiple lines in a file, if not exisits i want to add those lines in that file
    Here is example:
    [root@oraclelinux6 tmp]# cat /etc/pam.d/password-auth
    #%PAM-1.0
    # This file is auto-generated.
    # User changes will be destroyed the next time authconfig is run.
    auth required pam_env.so
    auth sufficient pam_unix.so nullok try_first_pass
    auth requisite pam_succeed_if.so uid >= 500 quiet
    auth required pam_deny.so

    by sung the below command i have searched multiple lines but its not worked

    [root@oraclelinux6 tmp]# egrep “auth required pam_env.so|auth required pam_faillock.so preauth audit silent deny=5 unlock_time=900|auth [success=1 default=bad] pam_unix.so|auth [default=die] pam_faillock.so authfail audit deny=5 unlock_time=900|auth sufficient pam_faillock.so authsucc audit deny=5 unlock_time=900|auth required pam_deny.so” /etc/pam.d/password-auth
    [root@oraclelinux6 tmp]#

    Can please help assist me grep those lines if not add these lines in that file.

    Thanks in advance..

    Raju

  21. Touche. ?reat arguments. ?eep up the great
    effort.

Leave a Reply