Results 1 to 8 of 8

Thread: grep help, please

  1. #1
    Join Date
    Nov 2009
    Location
    Gainesville, VA
    Beans
    459
    Distro
    Ubuntu Studio 12.04 Precise Pangolin

    grep help, please

    Hello,

    I have a samba log file I'm trying to pull data from (/var/log/samba/log.combined). Specifically, I am trying to pull all lines from the file where a specific IP is mentioned

    Code:
    less log.combined | grep 10.1.4.* > file1
    However, this particular log file actually has two lines per data instance. Example:

    Code:
    [2012/04/12 03:03:46, 3] nmbd/nmbd_incomingrequests.c:process_name_query_request(454)
    process_name_query_request: Name query from 10.1.4.54 on subnet 10.1.0.115 for name WPAD<00>
    So, my grep returns data from the third line, but I also need the first line to know when said IP attempted access. How do I pull this off?

    Thanks!
    Last edited by Diametric; April 18th, 2012 at 06:50 PM.

  2. #2
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: grep help, please

    Try this:

    Code:
    grep 10.1.4 log.combined > file1
    Can you show a few sample lines from the log file?

  3. #3
    Join Date
    Nov 2009
    Location
    Gainesville, VA
    Beans
    459
    Distro
    Ubuntu Studio 12.04 Precise Pangolin

    Re: grep help, please

    It's there, I just didn't wrap code filter around the instance.

  4. #4
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: grep help, please

    There should be a way to use awk to grab a line with a pattern along with the preceding line.

  5. #5
    Join Date
    Nov 2009
    Location
    Gainesville, VA
    Beans
    459
    Distro
    Ubuntu Studio 12.04 Precise Pangolin

    Re: grep help, please

    Thanks for your replies - I just don't know enough about awk to pull that off in a reasonable amount of time. Hell...I'm proud I was able to get that grep command to work!

  6. #6
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: grep help, please

    grep -B2 ...
    -Bn include n lines before matching line
    -An include n lines after matching line

    example
    Code:
    $ echo $'1\n2\n3\n4\n5'
    1
    2
    3
    4
    5
    $ echo $'1\n2\n3\n4\n5' | grep -A2 '3'
    3
    4
    5
    $ echo $'1\n2\n3\n4\n5' | grep -B2 '3'
    1
    2
    3
    $ echo $'1\n2\n3\n4\n5' | grep -B1 -A1 '3'
    2
    3
    4
    Last edited by Vaphell; April 18th, 2012 at 07:04 PM.

  7. #7
    Join Date
    Nov 2009
    Location
    Gainesville, VA
    Beans
    459
    Distro
    Ubuntu Studio 12.04 Precise Pangolin

    Re: grep help, please

    Quote Originally Posted by Vaphell View Post
    grep -B2 ...
    -Bn include n lines before matching line
    -An include n lines after matching line
    Perfect! Thank you so much, I have exactly what I was looking for.

    Cheers.

  8. #8
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: grep help, please

    Quote Originally Posted by Vaphell View Post
    grep -B2 ...
    -Bn include n lines before matching line
    -An include n lines after matching line
    That's awesome!

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
  •