SED command help

Hello,

I have a script that executes a batch job within our application and then e-mails the log file. What I'm looking to do is to use the sed command to extract a value from this log file and set it as a variable, and then do further processing.

I've searched and read several things on using sed, but I guess I'm just not understanding how it works.

This is what I've formed so far...

ErrCnt=`cat taic.log | sed -n s/         /p'

This is what the taic.log looks like:

Batch process TAIC started
Running Processes
Processes completed, 0 errors found
Batch process TAIC completed

So, I'm trying to extract the number of errors found as a variable. Any assistance would be appreciated.

Thanks.

I'm not sure what you want, but this counts the number of errors:

awk '{if(/^Process/) { total+=$3}} END { print total}' filename

input:

Batch process TAIC started
Running Processes
Processes completed, 0 errors found
Batch process TAIC completed
Batch process TAIC started
Running Processes
Processes completed, 1 errors found
Batch process TAIC completed
Batch process TAIC started
Running Processes
Processes completed, 0 errors found
Batch process TAIC completed

output:

1

in a variable:

errcnt=`awk '{if(/^Process/) { total+=$3}} END { print total}' filename`

I appreciate your response!

I put the following into a separate script (taic2.sh) to verify that the variable is being set properly:

errcnt=`awk '{if(/^Process/) { total+=$3}} END { print total}' taic.kt.log`
print $errcnt

And I received this as the response:

$ ./taic2.sh
            
$           

Any thoughts has to why this isn't returning anything? I've never looked into awk, so I'm not sure of how to check the syntax. thanks!

Using awk for such a purpose is like using H-bombs for chasing blackbirds away.

As i take it from your explanation of the problem you want to find the line containing "Processes completed, XX errors found" where XX could be any number. Then you want to get this value of XX into a variable for further processing.

I suppose here for the sake of simplicity that the file cannot contain more than one line with "Processes completed....". If this would be the case the solution would need a loop to process one value after the other. Pls. check back if this is the case and I'll expand on this. Here we go:

We want to find a line that contains "Processes completed,", then some integer value, followed by " errors found". We want to ignore all the other lines in the file and reduce this line to only the integer value. This is the basis of our regular expression:

sed -n 's/^Processes completed, \([0-9][0-9]*\) errors found/\1/p' taic.log

The '-n'-option tells sed not to print any lines until explicitly being told so by the "p" option. Now we have phrased our substitute-expression in a way, where only the one line we look for fits in, so this is the only one which will get printed. within the line we define a variable ("\(....\)") and in it we catch every number there might be. We replace the comlete line by only the numbers in this variable ( .../\1/....) and hence print only them to stdout.

The next part is to catch this output into a shell variable. We do this by issuing the sed-command in a subshell:

iErrors=$(sed -n 's/^Processes completed, \([0-9][0-9]*\) errors found/\1/p' taic.log)

Now the shell fires up a new shell, executes in this everything between "$(" and ")" and replaces the "$(...)" with the output of that command. This is our integer and after doing this the command really to be executed would be (in case the errors would be 10) "iErrors=10". Now the only task left is to tidy up by declaring all the involved variables. Taken them for granted is *yuck*, y'know? ;-))

#! /bin/ksh

typeset -i iErrors=$(sed -n 's/^Processes completed, \([0-9][0-9]*\) errors found/\1/p' taic.log)

if     [ $iErrors -eq 0 ] ; then
     # 0 errors found
elseif [ $iErrors -eq 1 ] ; then
     # 1 errors found
else
     # more than 1 errors fould
fi

....

I hope this helps.

bakunin

I will integrate into my script and test tomorrow as I'm walking about the door now. thank you for your help! :b: