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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use grep sed or awk to extract string from log file and put into CSV
# 1  
Old 08-16-2011
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:
Code:
File = foo.bat
Date = 11/11/11
User = Foo Bar
Size = 1024
...

CSV should look like:
Code:
"foo.bat","11/11/11","Foo Bar","1024"

# 2  
Old 08-16-2011
I'm guessing each time it finds 'File' it should assume it's the start of a new record?

Code:
#!/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


Last edited by Corona688; 08-16-2011 at 03:53 PM.. Reason: Remove extra commas, fix quoting
# 3  
Old 08-16-2011
Let's say that each log file looks like this:
Code:
     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:
Code:
"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.
# 4  
Old 08-16-2011
Ah, each individual file has only those records? Simple enough:
Code:
#!/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


Last edited by Corona688; 08-16-2011 at 04:14 PM..
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 08-16-2011
Yes, it should work. I'll give this a shot. Thank you for your help.

Last edited by chipperuga; 08-16-2011 at 04:13 PM.. Reason: Misunderstanding
# 6  
Old 08-16-2011
[edit] nevermind, misunderstanding
# 7  
Old 08-16-2011
The dir will look something like this:
Code:
/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:

Code:
"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"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/sed command to extract the string between 2 patterns but having some particular value

Hi - i have one file with content as below. ***** BEGIN 123 ***** BASH is awesome ***** END ***** ***** BEGIN 365 ***** KSH is awesome ***** END ***** ***** BEGIN 157 ***** KSH is awesome ***** END ***** ***** BEGIN 7123 ***** C is awesome ***** END ***** I am trying to find all... (4 Replies)
Discussion started by: reldb
4 Replies

2. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

3. Shell Programming and Scripting

Extract data from a log file and put it in a file

Hi, I would like to seek your help for a script that will extract data from log file and put it in a file. Sample log file 2018-10-23 12:33:21 AI ERROR -- tpid: SAMPLE_TH account: 123456789 aiSessionNumber: 660640464 mapName: xxx to yyy errorDesc: Translation Error:ErrorNumber : 993 ... (2 Replies)
Discussion started by: neverwinter112
2 Replies

4. Shell Programming and Scripting

sed / awk / grep to extract information from log

Hi all, I have a query that runs that outputs data in the following format - 01/09/12 11:43:40,ADMIN,4,77,Application Group Load: Name(TESTED) LoadId(5137-1-0-1XX-15343-15343) File(/dir/dir/File.T03.CI2.RYR.2012009.11433350806.ARD) InputSize(5344) OutputSize(1359) Rows(2) Time(1.9960)... (8 Replies)
Discussion started by: jeffs42885
8 Replies

5. Shell Programming and Scripting

How to extract start/end times from log file to CSV file?

Hi, I have a log file (log.txt) that which contains lines of date/time. I need to create a script to extract a CSV file (out.csv) that gets all the sequential times (with only 1 minute difference) together by stating the start time and end time of this period. Sample log file (log.txt) ... (7 Replies)
Discussion started by: Mr.Zizo
7 Replies

6. Shell Programming and Scripting

Router ping log extract data from it Awk/Sed/grep

Hi, I am new to this world.. Using expect i loging to router and checking ping response to my links. I need to genarate report using this output and that report contains only three file link name, packet loss, latency. my output of script is like below: -bash-3.00$ monmw/mwbkp... (2 Replies)
Discussion started by: jkmistry
2 Replies

7. Shell Programming and Scripting

Need help please with Grep/Sed command to extract text and numbers from a file

Hello All, I need to extract lines from a file that contains ALPHANUMERIC and the length of Alphanumeric is set to 16. I have pasted the sample of the lines from the text file that I have created. My problem is that sometimes 16 appears in other part of the line. I'm only interested to... (14 Replies)
Discussion started by: mnassiri
14 Replies

8. UNIX for Advanced & Expert Users

bash/grep/awk/sed: How to extract every appearance of text between two specific strings

I have a text wich looks like this: clid=2 cid=6 client_database_id=35 client_nickname=Peter client_type=0|clid=3 cid=22 client_database_id=57 client_nickname=Paul client_type=0|clid=5 cid=22 client_database_id=7 client_nickname=Mary client_type=0|clid=6 cid=22 client_database_id=6... (3 Replies)
Discussion started by: Pioneer1976
3 Replies

9. UNIX for Advanced & Expert Users

WEB Server Log File Analysis using awk/sed/grep

I'm trying to find a way to show large page sizes (page size in K) from multiple web server log files. Essentially I want to show only rows from a file where a specific column is larger than some value. Has anyone ever done this type of log analysis? If so, a snippet of code would be very... (2 Replies)
Discussion started by: mike_cataldo@ad
2 Replies

10. Shell Programming and Scripting

sed, grep, awk, regex -- extracting a matched substring from a file/string

Ok, I'm stumped and can't seem to find relevant info. (I'm not even sure, I might have asked something similar before.): I'm trying to use shell scripting/UNIX commands to extract URLs from a fairly large web page, with a view to ultimately wrapping this in PHP with exec() and including the... (2 Replies)
Discussion started by: ropers
2 Replies
Login or Register to Ask a Question