how to search reports with specified keyword in log


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to search reports with specified keyword in log
# 1  
Old 02-22-2009
how to search reports with specified keyword in log

Hi,

I have a question with sed/awk. When I handle some log files I want to search all reports with specified keyword. For example, in the log below.

abcd
efg
===start
abc
e
===end
xyz
===start
af
f
===end
nf
ga
===start
ab
===end

In this log I want to get all reports(started with "===start", ended with "===end") which have keywords "ab", in this case

===start
abc
e
===end
===start
ab
===end

But I don't know how to make it. Can anyone please help with this?

I tried the following but it doesn't work:

sed -n -e "/===start/,/===end/ {
: _again
N;
/===end/ b _ready
b _again
: _ready
/ab/ p;
}" 123 > $_TMP_OUT

It produce not only the needed report but also other logs.

I am using OpenSUSE 10. The sed version is "GNU sed version 4.1.4".

Please help. Thanks a lot in advance.

Daniel
# 2  
Old 02-22-2009
This awk script should work

Code:
BEGIN { PRT="N"; START="N" }
/\=\=\=start/ { START="Y"; PRT="N" ; CNT=0 }
/\=\=\=end/ { if ( PRT == "Y" && START == "Y" ) { for ( i=1; i <= CNT; i++ ) print a[i] ; print $0 }}
/ab/ { PRT="Y" }
{ CNT++; a[CNT]=$0 }

Run as awk -f <file.awk> <datafile>

Gives output:

Quote:
===start
abc
e
===end
===start
ab
===end
# 3  
Old 02-22-2009
This should work
Code:
awk '/===end/{a=a OFS $0;if(g)print a;f=g=0}f{if($0 ~ KEY)g=1;a=a OFS $0}/===start/{f=1;a=$0}' OFS='\n' KEY=ab file

# 4  
Old 02-23-2009
try this perl script:
Code:
# search_report.pl
my $filename = shift;
my $all_file;
open (FH, '<', $filename)  or  die "Failed to read file $filename : $! \n";
{
  local $/;    # Enable "slurp" mode
  $all_file = <FH>;
}
close FH;

while ($all_file =~ m/(===start(.*?)===end)/gs) {
  my $this_block = $1;
  print $this_block, "\n\n"  if ($this_block =~ m/ab/);
}

run this script as:
Code:
perl search_report.pl filename.log

# 5  
Old 02-23-2009
Thank you all for your nice help.

I am using the first solution from pmm now since it is much easier for me to understand. It works well.

Thanks again.
Daniel
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. What is on Your Mind?

Search Results for the UNIX keyword - Google, Bing, DuckDuckGo

Some search results for the keyword "unix" searches: DuckDuckGo #1 https://www.unix.com/members/1-albums215-picture1254.png Bing #2 https://www.unix.com/members/1-albums215-picture1253.png Google #15 (page 2) https://www.unix.com/members/1-albums215-picture1252.png (1 Reply)
Discussion started by: Neo
1 Replies

2. Shell Programming and Scripting

Search for a Keyword in file and replace another keyword or add at the end of line

Hi I want to implement something like this: if( keyword1 exists) then check if(keyword2 exists in the same line) then replace keyword 2 with New_Keyword else Add New_Keyword at the end of line end if eg: Check for Keyword JUNGLE and add/replace... (7 Replies)
Discussion started by: dashing201
7 Replies

3. Shell Programming and Scripting

Keyword search/replace for two text files?

What is the best way (bash/awk/sed?) to read in two text files and do a keyword search/replace? file1.txt: San Francisco Los Angeles Seattle Dallas file2.txt: I love Los Angeles. Coming to Dallas was the right choice. San Francisco is fun. Go to Seattle in the summer. ... (3 Replies)
Discussion started by: pxalpine
3 Replies

4. Shell Programming and Scripting

Shell script to search a keyword in six different servers

Hello, I need a shell script which takes search keyword as input and then searches logs in six different servers and provide me the logs where in it found the keyword. Can anyone help???? (1 Reply)
Discussion started by: tomlui2010
1 Replies

5. Solaris

Keyword search input from a file

Hi, I have a file which got only one column and got some keywords. I have another file where the keywords used in the first file are repeated in the second file. Now I would like to know how many times each keyword from the first file is repeated in the second file. Request your help on... (1 Reply)
Discussion started by: pointers
1 Replies

6. Linux

Generating apache log reports

Hello all, I'm trying to find some tool on generating reports based on apache access_log files (of Common format). I found some of them (awstats, lire/logreport, weblog expert, apache logs viewer, etc..) but they generate some global and general report about the log file. Also some perl... (0 Replies)
Discussion started by: enux
0 Replies

7. Shell Programming and Scripting

compare two files and search keyword and print output

You have two files to compare by searching keyword from one file into another file File A 23 >pp_ANSWER 24 >aa hello 25 >jau head wear 66 >jss oops 872 >aqq olps ploww oww sss 722 >GG_KILLER ..... large files File B Beta done KILLER John Mayor calix meyers ... (5 Replies)
Discussion started by: cdfd123
5 Replies

8. Shell Programming and Scripting

search for keyword in subsequent lines and delete the second line

I have my data something like this I need to search for the keyword yyyy in the susequent lines and if it is present, delete the second line with keyword. In other words, if a keywords is found in two subsequent lines delete the second line. input data: aaaa bbbbb cccc dddd xxxx... (4 Replies)
Discussion started by: rdhanek
4 Replies

9. Shell Programming and Scripting

multiple search keyword in grep

Dear All, I have a file containing info like TID:0903 asdfasldjflsdjf TID:0945 hjhjhkhkhkh TID:2045 hjhjhkhkhkh TID:1945 hjhjhkhkhkh TID:2045 hjhjhkhkhkh I need to show only lines containing TID:0903 asdfasldjflsdjf TID:0945 hjhjhkhkhkh TID:2045 hjhjhkhkhkh TID:2045 hjhjhkhkhkh ... (11 Replies)
Discussion started by: saifurshaon
11 Replies

10. Shell Programming and Scripting

how to search a keyword within a file using a for loop

hi guys i have a problem here, im trying to stablish a relationship between a text file and an input user for example the script is going to prompt the user for some football team and what the script is going to do is return the colums in which that input is located so far this is what i have ... (6 Replies)
Discussion started by: lucho_1
6 Replies
Login or Register to Ask a Question