Print text it contains a certain term...

Hello,

I need to extract execution error on a log file. I am on Linux, but I can only use bash or ksh.

As a sample of what I'm needing, here is some data;

***** L1 FILE VAX_ASP1_SATx_E_IM20101017035246 - CPU TIME 2011-04-29T09:57:57 *****
GMT_PRODUCT_CREATION_TIME = 2011-04-29T09:57:57
Execution OK
***** L1 FILE VAX_ASP1_SATx_E_IM20101017162551 - CPU TIME 2011-04-29T09:59:51 *****
ERROR : Cannot open /DATA/ASPERA4//DATA/AU/PAHLR.asc PAHLR file
GMT_PRODUCT_CREATION_TIME = 2011-04-29T09:59:51
Execution OK
***** L1 FILE VAX_ASP1_SATx_E_IM20101018034316 - CPU TIME 2011-04-29T10:00:37 *****
GMT_PRODUCT_CREATION_TIME = 2011-04-29T10:00:37
Execution OK
***** L1 FILE VAX_ASP1_HKxx_E_RT20110531220100 - CPU TIME 2011-09-14T19:18:00 *****
GMT_PRODUCT_CREATION_TIME = 2011-09-14T19:18:00
Execution OK

The general idea is that I want to "search" a bit of text like this for certain terms, such as "Content-Length:" but also to be able to filter by "date" as I do not want to see all error that contain the log file and receive the following as the result ..

***** L1 FILE VAX_ASP1_SATx_E_IM20101017162551 - CPU TIME 2011-04-29T09:59:51 *****
ERROR : Cannot open /DATA/ASPERA4//DATA/AU/PAHLR.asc PAHLR file
GMT_PRODUCT_CREATION_TIME = 2011-04-29T09:59:51
Execution OK

Thanks for your help

Hi,

Try next 'perl' script. It outputs that part of the log with the 'ERROR' word in it.

$ cat infile
***** L1 FILE VAX_ASP1_SATx_E_IM20101017035246 - CPU TIME 2011-04-29T09:57:57 *****
GMT_PRODUCT_CREATION_TIME = 2011-04-29T09:57:57
Execution OK
***** L1 FILE VAX_ASP1_SATx_E_IM20101017162551 - CPU TIME 2011-04-29T09:59:51 *****
ERROR : Cannot open /DATA/ASPERA4//DATA/AU/PAHLR.asc PAHLR file
GMT_PRODUCT_CREATION_TIME = 2011-04-29T09:59:51
Execution OK
***** L1 FILE VAX_ASP1_SATx_E_IM20101018034316 - CPU TIME 2011-04-29T10:00:37 *****
GMT_PRODUCT_CREATION_TIME = 2011-04-29T10:00:37
Execution OK
***** L1 FILE VAX_ASP1_HKxx_E_RT20110531220100 - CPU TIME 2011-09-14T19:18:00 *****
GMT_PRODUCT_CREATION_TIME = 2011-09-14T19:18:00
Execution OK
$ cat script.pl
use warnings;
use strict;

my $extracted_text;
while ( <> ) {
        if ( (my $begin = /^\*\*\*\*\*/) .. (my $end = /^(?i:execution)\s+/) ) {
                if ( $begin ) { 
                        $extracted_text = ""; 
                }
                $extracted_text .= $_;
                if ( $end && $extracted_text =~ /(?i:ERROR)/s ) {
                        printf "%s", $extracted_text;
                }
        }
}
$ perl script.pl infile
***** L1 FILE VAX_ASP1_SATx_E_IM20101017162551 - CPU TIME 2011-04-29T09:59:51 *****
ERROR : Cannot open /DATA/ASPERA4//DATA/AU/PAHLR.asc PAHLR file
GMT_PRODUCT_CREATION_TIME = 2011-04-29T09:59:51
Execution OK

Regards,
Birei

1 Like

Thanks, but as I can't use perl.

I can only use bash or ksh.

Thanks for your help

Try Sed..

sed -n 'H;/Execution OK/{x;/ERROR/s/[^\n]*\n//p}' inputfile
1 Like

Thanks michaelrozar17, your code is working great .... but do you know how I can use a date filter.

If I use your line code with "grep" it doesn't work of course ... I was looking to print execution error but only with a specific date (yyyy-mm-dd)

sed -n 'H;/Execution OK/{x;/ERROR/s/[^\n]*\n//p}' |  grep 2011-06-17 | sat.log

Thanks for your help

Then declare a variable to hold the date value and try..

DT='2011-06-17'
sed -n 'H;/Execution OK/{x;/ERROR.*'"$DT"'/s/[^\n]*\n//p}' inputfile
1 Like

You are a real master ... thank you so much .... Problem solved :b: