Use grep sed or awk to extract string from log file and put into CSV

I'd like to copy strings from a log file and put them into a CSV.

The strings could be on different line numbers, depending on size of log.

Example Log File:

File = foo.bat
Date = 11/11/11
User = Foo Bar
Size = 1024
...

CSV should look like:

"foo.bat","11/11/11","Foo Bar","1024"

I'm guessing each time it finds 'File' it should assume it's the start of a new record?

#!/bin/sh

# usage:  script.sh < infile  > outfile

PFIX=

while IFS=" =" read VAR VALUE
do
        if [ "$VAR" = "File" ] && [ ! -z "$PFIX" ]
        then
                # Print a newline
                echo
                # First record on a line has no comma in front
                PFIX=""
        fi

        printf "%s\"%s\"" "$PFIX" "$VALUE"
        PFIX=","
done

[ ! -z "$PFIX" ] && echo

Let's say that each log file looks like this:

     licensed to = Client X
     packaged by = Bill Gates
     custodian = Steve Jobs
     Time Zone = Eastern Standard Time
     session name = HDD1-20110613
     matter = XBOX
     engagement = 
     system name = XXYY
     user name = gates1
     os version = Microsoft Windows NT 5.1.2600 Service Pack 3
     processor count = 4
     File = foo.bat
     Date = 11/11/11
     User = Foo Bar
     Size = 1024

I'd like to extract only the following into a line on a CSV:

"foo.bat","11/11/11","Foo Bar","1024"

There will be numerous log files that are contained in the same directory. But each log will have the same variables and different data.

Ah, each individual file has only those records? Simple enough:

#!/bin/sh

# usage:  script.sh file1 file2 file3 ...

while [ "$#" -gt 0 ]
do
        PFIX=
        while IFS=" =" read VAR VALUE
        do
                case "$VAR" in
                File|Date|User|Size)
                        printf "%s\"%s\"" "$PFIX" "$VALUE"
                        PFIX=","
                        ;;        
               *)
                        ;;
                esac
        done < "$1"

        [ ! -z "$PFIX" ] && echo
        shift
done
1 Like

Yes, it should work. I'll give this a shot. Thank you for your help.

[edit] nevermind, misunderstanding

The dir will look something like this:

/var/log/foologs/
..
  foo1.log
  foo2.log
  foo3.log
  foo4.log

I'd like the script to go into each log file, and then append to the CSV.

So if there are 4 log files in the directory, the CSV should look like:

"foo.bat","11/11/11","Foo Bar","1024"
"foobar.bat","12/31/11","Foo Bar","2048"
"foo","8/2/09","Foo Bar","512"
"fooless.bat","6/11/08","Foo Bar","2048"

So do

./script.sh dir/*.log