Find the answer to your Linux question:
Results 1 to 3 of 3
Hi all. Hoping someone can help me get down the last mile with some log file manipulation. Here's the scenario. When confronted with the pattern below in a log file, ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2007
    Posts
    1

    File manipulation assitance


    Hi all. Hoping someone can help me get down the last mile with some log file manipulation.

    Here's the scenario. When confronted with the pattern below in a log file, I am able to successfully create single lines for each entry.

    MyServer Fri Jan 26 23:01:27 2007
    INET/inet_error: read errno = 104

    MyServer Fri Jan 26 23:01:27 2007
    INET/inet_error: read errno = 104

    MyServer Fri Jan 26 23:01:27 2007
    INET/inet_error: read errno = 104

    MyServer Fri Jan 26 23:01:27 2007
    INET/inet_error: read errno = 104

    MyServer Fri Jan 26 23:01:27 2007
    INET/inet_error: read errno = 104

    To create the single lines, I use paste -s -d '\t\t\n' < input > output, leaving me with the format below which is perfect for pulling into a database and working with from there.

    MyServer Fri Jan 26 23:01:38 2007 INET/inet_error: read errno = 104
    MyServer Fri Jan 26 23:01:38 2007 INET/inet_error: read errno = 104
    MyServer Fri Jan 26 23:01:38 2007 INET/inet_error: read errno = 104
    MyServer Fri Jan 26 23:01:38 2007 INET/inet_error: read errno = 104
    MyServer Fri Jan 26 23:01:38 2007 INET/inet_error: read errno = 104

    My problem is whenever confronted with an entry in the error log that is more than two lines, my paste command falls down. See the example below.

    MyServer Fri Jan 26 23:01:27 2007
    INET/inet_error: read errno = 104
    error processing 3455980x

    What I am trying to achieve is something like the above, combining all of the text associated with a single entry into the error log into one continuous line.

    MyServer Fri Jan 26 23:01:38 2007 INET/inet_error: read errno = 104 error processing 3455980x

    Apologies for the lengthy post, any help would be greatly appreciated.

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    If entries are separated by empty lines then the way to do it is
    Code:
    gawk '{ if ($0 ~ /^$/) {print line ; line = ""} else {line = (line == "" ? $0 : line " " $0)} } END {print line}' log_file

  3. #3
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Try this:

    Code:
    #!/bin/sh
    
    while read line
    do
      if [ "$line" != "" ]; then
        if [ ${line:0:8} == "MyServer" -a "$str" != "" ] ; then
            echo $str
            str=""
          fi
        str=`echo $str $line`
      fi
    done < log_file
    
    echo $str
    Regards

  4. $spacer_open
    $spacer_close

Posting Permissions

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