#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    17
    Rep Power
    0

    UNIX script for searching in archives and logs


    Hello

    I'm trying to write a script that can search in log files for a specific text and write down the line in a txt file. Log files are archived once every 2 days, so i need to search in archived files also.

    Something like:

    Code:
    -bash-3.2$ ssh server.com
    -bash-3.2$ cd test/log/
    less server.log.2012-06-19.gz | grep "text" -> ~/test.txt
    I'm kind of a newbie in UNIX
    Thanks
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
    Test this little script out:
    Code:
    # test_search_logs.sh
    # Parm 1 is search string
    # Parm 2 is /path/file to write records to
    cd test/log
    for x in `ls server.log.*.gz`
    do
      # Get file name of unzipped file
      file=`echo ${x} | sed 's/.gz//'`
      # unzip file
      gzip -d ${file}.gz
      # Perform search and write found lines to file
      grep ${1} ${file} >${2}
      # gzip file again
      gzip -9f ${file}
    done

IMN logo majestic logo threadwatch logo seochat tools logo