0

i have written awk to print specyfic lines from space-delimited log file.

if [ ! -f $userlog ]
then
awk -F' ' '$7$8 == "Loginitialized" {print $2" "$3" "$4" "$5" process start"}''$7$8$9 == "Applictionsuccessfullyended" {print $2" "$3" "$4" "$5" process end"}' $preplog > $userlog
fi

output sample (userlog)

2015-03-02 13:14:25 (PID 19594) process start
2015-03-02 22:42:29 (PID 30473) process start
2015-03-02 22:53:20 (PID 30473) process end
2015-03-03 07:16:55 (PID 31078) process start
2015-03-03 14:53:15 (PID 16591) process start
2015-03-03 14:54:10 (PID 18292) process start

I need to use same awk on $preplog but from last line i've printed with it. i was trying with that but i failed:

if [ -f $userlog ]
then
lastpid=`awk -F' ' 'END{print $4}' $userlog`        #pid ostatniego procesu
cat $preplog | awk 'BEGIN{ found=0} /PID /$lastpid/ Log initialized/{found=1}  {if (found) {awk -F" " "$7$8 == "Loginitialized" {print $2" "$3" "$4" "$5" process start"}''$7$8$9 == "Applictionsuccessfullyended" {print $2" "$3" "$4" "$5" process end"}"} ;}' >> $userlog
fi

but my awk programing is not strong in me. I literaly have no idea how to bite it.

sander0s
  • 1
  • 1
  • 1

1 Answers1

1

Let's start by cleaning up you current script:

awk -F' ' '$7$8 == "Loginitialized" {print $2" "$3" "$4" "$5" process start"}''$7$8$9 == "Applictionsuccessfullyended" {print $2" "$3" "$4" "$5" process end"}' $preplog > $userlog

Change that to:

awk '
   $7$8 == "Loginitialized" { state="start" }
   $7$8$9 == "Applictionsuccessfullyended" { state="end" }
   state { print $2, $3, $4, $5, "process", state; state="" }
' "$preplog" > "$userlog"

for improved robustness and clarity, and to remove unnecessary constructs and redundancy.

For what you want to do next though - see if reading https://stackoverflow.com/a/18409469/1745001 helps and if not edit your question to show sample input and expected output plus a clearer, more detailed explanation of what you're trying to do.

Hang on, I think I just had a revelation after re-reading your attempted code segment - MAYBE what you want is to only analyze and operate on the last line of the input file instead of every line of it. If so that'd be:

awk '
   { var1=$7$8; var2=$7$8$9 }
   END {
       if (var1 == "Loginitialized") { state="start" }
       if (var2 == "Applictionsuccessfullyended") { state="end" }
       if (state) { print $2, $3, $4, $5, "process", state; state="" }
   }
' "$preplog" > "$userlog"

If that's not what you wanted you'll need to provide more information.

Community
  • 1
  • 1
Ed Morton
  • 188,023
  • 17
  • 78
  • 185