Print text it contains a certain term...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print text it contains a certain term...
# 1  
Old 09-16-2011
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;

Code:
***** 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 ..

Code:
***** 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
# 2  
Old 09-16-2011
Hi,

Try next 'perl' script. It outputs that part of the log with the 'ERROR' word in it.
Code:
$ 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
This User Gave Thanks to birei For This Post:
# 3  
Old 09-19-2011
Thanks, but as I can't use perl.

I can only use bash or ksh.

Thanks for your help
# 4  
Old 09-19-2011
Try Sed..
Code:
sed -n 'H;/Execution OK/{x;/ERROR/s/[^\n]*\n//p}' inputfile

This User Gave Thanks to michaelrozar17 For This Post:
# 5  
Old 09-19-2011
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)

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

Thanks for your help
# 6  
Old 09-19-2011
Then declare a variable to hold the date value and try..
Code:
DT='2011-06-17'
sed -n 'H;/Execution OK/{x;/ERROR.*'"$DT"'/s/[^\n]*\n//p}' inputfile

This User Gave Thanks to michaelrozar17 For This Post:
# 7  
Old 09-19-2011
You are a real master ... thank you so much .... Problem solved Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Match text and print/pipe only that text

I'm trying to pull an image source url from a html source file. I'm new with regex. I'm in BaSH. I've tried grep -E 'http.*jpg' file which highlights the text, but gives me 2 problems: 1) Results aren't stand alone and can't be piped to another command. (I believe it includes everything in... (5 Replies)
Discussion started by: amx401
5 Replies

3. UNIX for Dummies Questions & Answers

How to create a print filter that print text & image?

Currently, I have a print filter that takes a text file, that convert it into PCL which then gets to a HP printer. This works. Now I need to embedded a image file within the text file. I'm able to convert the image file into PCL and I can cat both files together to into a single document... (1 Reply)
Discussion started by: chedlee88-1
1 Replies

4. Shell Programming and Scripting

awk based script to print the "mode(statistics term)" for each column in a data file

Hi All, Thanks all for the continued support so far. Today, I need to find the most occurring string/number(also called mode in statistics terminology) for each column in a data file (.csv type). For one column of data(1.txt) like below Sample 1 2 2 3 4 1 1 1 2 I can find the mode... (6 Replies)
Discussion started by: ks_reddy
6 Replies

5. Shell Programming and Scripting

Print text between delimiters IF it contains a certain term...

So I'm racking my brain on appropriate ways to solve a problem that once fixed, will solve every problem in my life. Its very easy (for you guys and gals) I'm sure, but I can't seem to wrap my mind around the right approach. I really want to use bash to do this, but I can't grasp how I'm going to... (14 Replies)
Discussion started by: eh3civic
14 Replies

6. AIX

Print text between two delimiters

Hi, Can somebody help me with the below situation, Input File, ======== 2007_08_07_IA-0100-014_(MONTHLY).PDF 2007_08_07_IA-0100-031_(QUARTERLY)(RERUN).PDF 2008-02-28_KR-1022-003_(MONTH)(RERUN)(REC1).CSV Required output, ============ MONTHLY QUARTERLY MONTH ... (15 Replies)
Discussion started by: sravicha
15 Replies

7. Shell Programming and Scripting

process text between pattern and print other text

Hi All, The file has the following. =========start of file=== This is a file containing employee info START name john id 123 date 12/1/09 END START name sam id 4234 date 12/1/08 resigned END (9 Replies)
Discussion started by: vlinet
9 Replies

8. Shell Programming and Scripting

Search text from a file and print text and one previous line too

Hi, Please let me know how to find text and print text and its previous line. Please don't get irritated few days back I asked text and next line. I am using HP-UX 11.11 Thanks for your help. (6 Replies)
Discussion started by: kamranjalal
6 Replies

9. Shell Programming and Scripting

Search term and output term in desired field

Hi All, I have an input_file below and i would like to use Perl to search for the term "aaa" and output the 3rd term in the same row as "aaa".For Example, i want to search for the term "ddd" and would want the code to ouput the 3rd term in the same row which is "fff". Can somebody help ? ... (28 Replies)
Discussion started by: Raynon
28 Replies

10. Programming

Create a Term & Run chars on this Term

hi floks ! i'd like to know how can i transmete a character or a string from my source code to a term and make it interpret or un by the shell wich is running in my term. I'd like to create a Term from my code (and get its file descriptor) and then transmete each char typed on the keyboard to... (1 Reply)
Discussion started by: the_tical
1 Replies
Login or Register to Ask a Question