Find the answer to your Linux question:
Results 1 to 4 of 4
Hi, this is my first post as I'm just starting to learn about grep and all it's powers since my company purchased Linux based software for our proxy server. Anyways, ...
  1. #1
    Just Joined!
    Join Date
    Jan 2008
    Posts
    2

    Script parse Squid logs

    Hi, this is my first post as I'm just starting to learn about grep and all it's powers since my company purchased Linux based software for our proxy server.

    Anyways, I'm trying to write a script that I can use to parse the squid logs.

    The logs look like this:
    Code:
    2007.12.30 4:24:26 - xx.xx.xx.xx http://url.url.com/
    2008.1.3 8:42:15 - xx.xx.xx.xx http://url.url.com/
    It would be great if I could have a way to enter a cetain date and ip and parse that out into a file.

    Right now I only know how to get the lines matching an IP, but I'd like to match an IP and Date. The time is irrelevant to me.

    Code:
    grep 'xx\.xx\.xx\.xx' file >> todaysdate.log
    Thats what I'm doing now. How can I improve on this or even have a script that I can update the IP and Date I want and have it create a new log file naming it the current date.

    Thanks in advance for any help.

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    If awk is allowed:

    Code:
    #!/bin/sh
    
    awk -v date="$1" -v ip="$2" '
    $1 == date && $2 == ip {print}
    ' logfile > todaysdate.log
    Usage: <scriptname> <date> <IP>

    Regards

  3. #3
    Just Joined!
    Join Date
    Jan 2008
    Posts
    2

    Thanks

    Hey fantastic! Thanks a bunch, it'll be a big help.

    One thing I had hoped, is there a way to add into that, instead of always naming the output the same thing, to have it auto create the filename to the days actual current date?

  4. #4
    Linux Guru anomie's Avatar
    Join Date
    Mar 2005
    Location
    Texas
    Posts
    1,692
    Code:
    [fugu ~]$ outfile=some-file-$(date +%F)
    [fugu ~]$ echo ${outfile}
    some-file-2008-01-06
    You get the idea. Initialize 'outfile' in your script, and then redirect to ${outfile}.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...