Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Editing log file using AWK 2

Status
Not open for further replies.

mchilukuri

Programmer
Sep 9, 2009
3
US
I am searching for particular line in log directory which has files generated daily for each job. I was able to extract the lines and the output looks like below

grep 'Session statistics: Requests' *

DE_CRAMERCompSys.200909081 0735711.dbg:[Mon Sep 7 2009 01:48:17.5820] - <TID: 001> <STATE > Session statistics: Requests added: 0, updated: 0, deleted: 3, errors: 0, warnings: 12442
DE_CRAMERCompSys.200909091 1116326.dbg:[Tue Sep 8 2009 01:08:20.4780] - <TID: 001> <STATE > Session statistics: Requests added: 0, updated: 0, deleted: 3, errors: 0, warnings: 12442
DE_CRAMERCompSys.dbg:[Wed Sep 9 2009 01:11:51.0430] - <TID: 001> <STATE > Session statistics: Requests added: 0, updated: 0, deleted: 3, errors: 0, warnings: 12442

Now I am trying to generate a file from the above file using AWK
Job Time Added Updated Deleted Errors
---- ------------- ------ ------- ------- ------
DE_CRAMERCompSys Mon Sep 7 2009 01:48:17 0 0 3 0
DE_CRAMERCompSys Tue Sep 8 2009 01:08:20 0 0 3 3
DE_CRAMERCompSys Wed Sep 9 2009 01:11:51 0 0 3 0

Can anyone please help as I am not that familiar with AWK
 
Have you tried anything so far? Where are you stuck?

This should give you some ideas:

Code:
awk '
    /Session statistics: Requests/ {
        # strip suffix off filename
        sub("[.].*","",FILENAME)
        # remove all commas in the line
        gsub(",","",$0)
        # print out the job name and error count
        printf "%-30s %6d\n", FILENAME, $(NF-2)
    }
' *

Annihilannic.
 
Adding to your grep (but of course awk can do the grep):

Code:
grep 'Session statistics: Requests' * | awk '
BEGIN {
    FieldFormat = "%-30s %-25s %6s %7s %7s %6s\n"
    printf(FieldFormat,"Job","Time","Added","Updated","Deleted","Errors")
    printf(FieldFormat,"----------------------------","-------------------------","------","-------","-------","------")
}

{
    # remove all commas in the line
    gsub(",","",$0)
    # split into 3 parts by timestamp
    n = split($0,parts,"[][]")
    # ignore line if in wrong format
    if(n != 3) next
    # get the parts
    Job = parts[1]
    Time = parts[2]
    Counts = parts[3]
    # clean up the counts
    sub("^.*added:","added:",Counts)
    # split the counts
    n = split(Counts, cp, " ")
    # ignore line if in wrong format
    if(n != 10) next
    # remove portion of secs
    sub("[.].*","",Time)
    # strip suffix off filename
    sub("[.].*","",Job)
    # print out the results
    printf(FieldFormat, Job, Time, cp[2], cp[4], cp[6], cp[8] )
}
'
 
I am getting following errors

awk: syntax error near line 10
awk: illegal statement near line 10
awk: syntax error near line 20
awk: illegal statement near line 20
awk: syntax error near line 26
awk: illegal statement near line 26
awk: syntax error near line 28
awk: illegal statement near line 28
 
I changed 'awk' to 'nawk' and I am not seeing any errors but it is not generating any output

It is just printing
Job Time Added Updated Deleted Errors
---------------------------- ------------------------- ------ ------- ------- ------
 
I guess you could try gawk instead.

For debug, replace
Code:
if(n != 3) next
with
Code:
if(n != 3) {
     printf("DEBUG: timestamp in wrong format\n")
     next
}
and
Code:
if(n != 10) next
with
Code:
if(n != 10) {
     printf("DEBUG: counts in wrong format\n")
     next
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top