Find the answer to your Linux question:
Results 1 to 5 of 5
Hi, I want to parse a log file like this: Code: Date Time Module Level Type Description 2010-04-20 14:18:42 system alert 00003 Multiple authentication failures have been detected! From 10.10.10.19 ...
  1. #1
    Just Joined!
    Join Date
    May 2009
    Posts
    9

    Question Bash - parsing

    Hi,

    I want to parse a log file like this:
    Code:
    Date       Time     Module Level  Type Description
    2010-04-20 14:18:42 system alert 00003 Multiple authentication failures have 
                                           been detected!  From 10.10.10.19                                       
                                           to 10.10.10.19, proto TCP (zone 
                                           Trust, int ethernet0/3). Occurred 1 
                                           times.
    2010-04-16 16:32:39 system alert 00003 Multiple authentication failures have 
                                           been detected!  From 10.17.10.10
                                           to 10.10.10.19, proto TCP (zone 
                                           Trust, int ethernet0/3). Occurred 1 
                                           times.
    [...]

    To get each record in one line, like this :
    Code:
    2010-04-20 14:18:42 system alert 00003 Multiple authentication failures have been detected!  From 10.10.10.19 to 10.10.10.19, proto TCP (zone Trust, int ethernet0/5). Occurred 1 times.
    The big problem is with the lines which begin after many spaces... I don't know how to manage them.

    I tried this to delete but that's do not works because they are space and not tabulation:
    Code:
    tr "\t" " "
    Thanks

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    452
    you're on the right track with "tr"--here's a kludge using "AAAAAA" as a
    marker, later replaced by newline. On the second line, the initial space
    resists autoremoval'file' for logfile name)

    sed '1d; s/times./times.AAAAAA/' <file | tr -t \\n " " | tr -s " " | tr AAAAAA \\n | tr -s \\n
    the sun is new every day (heraclitus)

  3. #3
    Linux Newbie
    Join Date
    Sep 2004
    Location
    UK
    Posts
    160
    Code:
    cat test.txt | sed 's/\s\+$//g' | \
      sed 's/^/_REC_START_/g' | \
      sed 's/_REC_START_\s\+/_SPACE_TO_REMOVE_/g' | \
      tr -d '\n' | sed 's/_SPACE_TO_REMOVE_/ /g' | \
      sed 's/_REC_START_/\n/g'
    produced

    Code:
    Date       Time     Module Level  Type Description
    2010-04-20 14:18:42 system alert 00003 Multiple authentication failures have been detected!  From 10.10.10.19 to 10.10.10.19, proto TCP (zone Trust, int ethernet0/3). Occurred 1 times.
    2010-04-16 16:32:39 system alert 00003 Multiple authentication failures have been detected!  From 10.17.10.10 to 10.10.10.19, proto TCP (zone Trust, int ethernet0/3). Occurred 1 times.
    Don't know if that helps.
    In a world without walls and fences, who needs Windows and Gates?

  4. #4
    Just Joined!
    Join Date
    May 2009
    Posts
    9
    @tpl Thank you for you help!

    @blinky That's not help, that's the solution :P Now I will do the necessary do understand your code.

    Thanks guys!

  5. #5
    Linux Newbie
    Join Date
    Sep 2004
    Location
    UK
    Posts
    160
    Sorry I was in a hurry (had to take my son to football training),

    First sed - remove trailing whitespace
    Second sed - place _REC_START_ at start of each line
    Third sed - Remove "_REC_START_ " with _SPACE_TO_REMOVE_ (sloppy could have just replaced with one space and not require forth sed)
    tr - remove end of line (\n)
    Forth sed - replace _SPACE_TO_REMOVE_ with single space
    Last sed - replace _REC_START_ with end of line (\n)
    In a world without walls and fences, who needs Windows and Gates?

Posting Permissions

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