I was breaking my head for a day still not successful. I have this script:#!/bin/kshfname=$1for batchname in $(grep -i Processing batch $fname | cut -d ' -f2)doBatch_state=`grep -c -i Batch '$batchname' was successful $fname`if [[ $Batch_state  -ge 1 ]];then{S_Time=`awk '/[0-9]_[0-9].*successful/{getline;getline;getline;print}' $fname | awk '{print $2}'`E_Time=`awk '/[0-9]_[0-9].*successful/{getline;getline;getline;getline;print}' $fname | awk '{print $2}'`echo -e $batchname\t$S_Time\t$E_Time}else{echo $batchname encountered an error}fidoneOutput this code is producing:02_1231324      14:29:04 15:29:11   14:32:19 15:33:1179_3097935      14:29:04 15:29:11   14:32:19 15:33:11Desired Output:02_1231324      14:29:04    14:32:1979_3097935      15:29:11    15:33:11 Sample Input:2013/06/11 14:29:04 <0999> (725102)Creating batch '02_1231324.0'...2013/06/11 14:29:04 <0999> (725102)Batch '02_1231324' was successful2013/06/11 14:29:04 <0999> (725102)TMR:Child ZERO, 160 Docs 320 Pgs 3874 KByts Tot 0.42 WAL 0.10 WALIO 0.15 IO 0.03 secs2013/06/11 14:29:04 <0999> (725102)  Processing batch '02_1231324'2013/06/11 14:32:19 <0999> (725102)Total in batch: 160 documents using 4 KBytes2013/06/11 15:29:11 <0999> (725102)Creating batch '79_3097935.0'...2013/06/11 15:29:11 <0999> (725102)Batch '79_3097935' was successful2013/06/11 15:29:11 <0999> (725102)TMR:Child ZERO, 160 Docs 320 Pgs 3874 KByts Tot 0.42 WAL 0.10 WALIO 0.15 IO 0.03 secs2013/06/11 15:29:11 <0999> (725102)  Processing batch '79_3097935'2013/06/11 15:33:11 <0999> (725102)Total in batch: 160 documents using 4 KBytesTMR:Child ZERO, 160 Docs 320 Pgs 3874 KByts Tot 0.42 WAL 0.10 WALIO 0.15 IO 0.03 secs2013/06/11 13:26:57 <0999> (725102)  Processing batch '12_2013162201'2013/06/11 13:26:57 <0999> (725102)Total in batch: 160 documents using 4 KBytesSo, what is wrong with my script? How can I get the desired output?
Reusing a variable
shell script;ksh;variable
You probably want something like this to parse your log:#!/bin/awk -f{    if (/was successful/) {        bn = $2;        gsub(/'/, , bn);        succ[bn] = 1;    }    if (/Processing batch/) {        bn = $7;        gsub(/'/, , bn);        if (bn in succ) {            succ[bn,,s] = $2;            getline            succ[bn,,e] = $2;        } else            fail[bn] = 1;    }}END {    for (val in succ)        if (val !~ /,/)            print val   succ[val,,s]   succ[val,,e];    for (val in fail)        print val  encountered an error;}Your original script slightly reduced to just 1 awk  call under some strict1 assumptions about the structure of your input data).#!/bin/kshfname=$1for batchname in $(grep -i Processing batch $fname | cut -d ' -f2); do    Batch_state=`grep -c -i Batch '$batchname' was successful $fname`    if [[ $Batch_state  -ge 1 ]]; then        awk -v b=$batchname '{    if ($0 ~ Processing.* b) {        s = $2;        getline;        e = $2;        print b   s   e;        exit    }}' $fname    else        echo $batchname encountered an error    fidoneShorter version using grep -A (-A is a GNU extension which selects the matching line and a number - 1 by default - of lines that follow):#!/bin/kshfname=$1for batchname in $(grep -i Processing batch $fname | cut -d ' -f2); do    Batch_state=`grep -c -i Batch '$batchname' was successful $fname`    printf %s $batchname    if [[ $Batch_state  -ge 1 ]]; then        grep -A1 Processing batch '$batchname' $fname | cut -f2 -d  | fmt    else        printf encountered an error\n    fidoneBut these are still processing the file 3 times. See ChuckCottrill's answer for how it could be done.1 it seems that the batches are not processed in parallel, and what you want is actually the line containing Processing batch and the line after that.