Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Some scripting help needed

  • 27-06-2008 08:25PM
    #1
    Registered Users, Registered Users 2 Posts: 6,946 ✭✭✭


    Hi guys, scripting isnt my strongpoint but im pretty sure it would be a cakewalk for a regular.

    I need a script to grep an IP address from the end of a log file, backwards until it finds 10 matches or hits the start of the logfile. The IP address that I am look for will change with each search of course. Output to screen is fine.

    Any ideas?


Comments

  • Registered Users, Registered Users 2 Posts: 52 ✭✭Jamar


    Perhaps I'm missing something, but

    grep '10.10.10.10' logfile | tail -10


  • Closed Accounts Posts: 1,462 ✭✭✭bushy...


    Maybe you could do something with tac ?
    #!/bin/bash
    tac   logfile | grep $1 | head -10
    


  • Registered Users, Registered Users 2 Posts: 868 ✭✭✭brianmc


    Jamar wrote: »
    Perhaps I'm missing something, but

    grep '10.10.10.10' logfile | tail -10

    I like this one.

    I'd change it to backslash the dots in the IP address though.

    Perhaps there's an option to grep to ignore regular expression characters?


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie


    brianmc wrote: »
    I like this one.

    +1. Clever.


  • Closed Accounts Posts: 37 Arion


    brianmc wrote: »
    I like this one.

    I'd change it to backslash the dots in the IP address though.

    Perhaps there's an option to grep to ignore regular expression characters?

    fgrep or grep -F


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 6,946 ✭✭✭SouperComputer


    Thanks for all the input guys. I went with tac in the end. So simple, cant believe Ive never come across it!


  • Registered Users, Registered Users 2 Posts: 37,485 ✭✭✭✭Khannie


    Just "man tac"'d. Never come across it before either. Useful to know.

    To save anyone else the hassle.....
    NAME
           tac - concatenate and print files in reverse
    
    SYNOPSIS
           tac [OPTION]... [FILE]...
    
    DESCRIPTION
           Write  each FILE to standard output, last line first.  With no FILE, or
           when FILE is -, read standard input.
    


Advertisement