Visit Our UNIX and Linux User Community


Grep word after last occurance of string and display next few lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep word after last occurance of string and display next few lines
# 1  
Old 11-28-2013
Grep word after last occurance of string and display next few lines

Hi,

I wanted to grep string "ERROR" and "WORNING" after last occurrence of String "Starting" only and wanted to display two lines after searched ERROR and WORNING string and one line before. I have following cronjob log file "errorlog" file and I have written the code for same in Unix as below and working fine but not able to implement it in SunSolari.

Please find errorlog file as below :

Code:
it is Starting 
line1
line2
ERROR
line3
line4

it is Starting
line5
ERROR
line6
line7

it is Starting
line8
WARNING
line8.8
line8.9
line9
ERROR
line10
line11

Expected output something like as follow :

Code:
line8
WARNING
line8.8

line9
ERROR
line10

Following code is working in UNIX/LINUX but not working in SunSolari

Code:
ERROR="ERROR:"
WARNING="WARNING" 
 ERR=$ERROR"\|"$WARNING

awk  '/Starting/{if(b) exit; else b=1}1' $filename | grep -A3 -in "$ERR"

Please advise Smilie

Last edited by Don Cragun; 11-30-2013 at 11:53 PM.. Reason: Remove extraneous font and size tags.
# 2  
Old 11-29-2013
Try:
Code:
nawk '/Starting/ { N=0 } {L[++N]=$0 } END { for(X=1; (X<=N)&&(X <= 3); X++) print L[X] }' inputfile

It keeps a list of lines in the array L, and every time it encounters 'Started', resets the count to zero and starts over. Adjust the <= 3 to taste.

Last edited by Corona688; 11-29-2013 at 12:01 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 11-29-2013
Hi.
Quote:
Originally Posted by nes
... grep string "ERROR" and "WORNING" after last occurrence of String "Starting" only and wanted to display two lines after searched ERROR and WORNING string and one line before ...
Using part of Corona688's solution:
Code:
#!/usr/bin/env bash

# @(#) s2       Demonstrate extraction of lines before and after match.

if uname -a | grep -i solaris 
then
  echo " ( Note -using ggrep )"
  G=/usr/sfw/bin/ggrep
else
  echo " ( Note -using grep )"
  G=grep
fi

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C nawk $G

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Expected output:"
cat expected-output.txt

pl " Results:"
nawk '/Starting/ { N=0 } {L[++N]=$0 } END { for(X=1; (X<=N); X++) print L[X] }' $FILE |
$G -E -A2 -B1 -i 'error|warning' 

exit 0

producing:
Code:
$ ./s2
SunOS vm-solaris 5.10 Generic_137138-09 i86pc i386 i86pc
 ( Note -using ggrep )

Environment: LC_ALL = POSIX, LANG = POSIX
(Versions displayed with local utility "version")
OS, ker|rel, machine: SunOS, 5.10, i86pc
Distribution        : Solaris 10 10/08 s10x_u6wos_07b X86
bash GNU bash 3.00.16
nawk - ( /usr/bin/nawk, Jan 8 2007 )
/usr/sfw/bin/ggrep grep (GNU grep) 2.5

-----
 Input data file data1:
it is Starting 
line1
line2
ERROR
line3
line4

it is Starting
line5
ERROR
line6
line7

it is Starting
line8
WARNING
line8.8
line8.9
line9
ERROR
line10
line11

-----
 Expected output:
line8
WARNING
line8.8

line9
ERROR
line10

-----
 Results:
line8
WARNING
line8.8
line8.9
line9
ERROR
line10
line11

See man pages for details.

Best wishes ... cheers, drl

Last edited by drl; 12-05-2013 at 08:44 AM.. Reason: Typo: corrected nawk line
This User Gave Thanks to drl For This Post:
# 4  
Old 11-29-2013
Try also:
Code:
tac file | awk '{T[NR%3]=$0} /ERROR|WARNING/ {L=NR+1; print T[(NR+1)%3];print T[(NR+2)%3]; print; next} NR<=L; /Starting/ {exit}'  |tac
line8
WARNING
line8.8
line8.9
line9
ERROR
line10
line11

This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-30-2013
I have given example of file, but not sure how many time "Starting" string is present in file.
I want only from last occurrence of string("Starting")to end of file where file has "ERROR" or "EXCEPTION".

Thanks Smilie
# 6  
Old 12-01-2013
Quote:
Originally Posted by nes
Hi,

I wanted to grep string "ERROR" and "WORNING" after last occurrence of String "Starting" only and wanted to display two lines after searched ERROR and WORNING string and one line before. I have following cronjob log file "errorlog" file and I have written the code for same in Unix as below and working fine but not able to implement it in SunSolari.

Please find errorlog file as below :

Code:
it is Starting 
line1
line2
ERROR
line3
line4

it is Starting
line5
ERROR
line6
line7

it is Starting
line8
WARNING
line8.8
line8.9
line9
ERROR
line10
line11

Expected output something like as follow :

Code:
line8
WARNING
line8.8

line9
ERROR
line10

Following code is working in UNIX/LINUX but not working in SunSolari

Code:
ERROR="ERROR:"
WARNING="WARNING" 
 ERR=$ERROR"\|"$WARNING

awk  '/Starting/{if(b) exit; else b=1}1' $filename | grep -A3 -in "$ERR"

Please advise Smilie
Could you please give me one example of a UNIX/LINUX system where the above sequence of commands produces the expected output you showed us above when given the sample errorlog file you provided above as input?

I would expect the above code to display a subset of the text starting with the 1st line of the file and ending on the line before the 2nd line in the file that contains the string "Starting"; not a subset of the text starting with the last occurrence of the string "Starting" and ending at the end of the file.
# 7  
Old 12-05-2013
Thanks for correction "Don Cragun",

Following code displays ERROR and WARNING from first 'Starting' occurance to second.

But I wanted from last to end of file in UNIX/LINUX also to solve this problem

Code:
ERROR="ERROR:"
WARNING="WARNING" 
 ERR=$ERROR"\|"$WARNING

awk  '/Starting/{if(b) exit; else b=1}1' $filename | grep -A3 -in "$ERR"

Please guide me.

Previous Thread | Next Thread
Test Your Knowledge in Computers #633
Difficulty: Medium
The touch and gesture features of the iPhone are based on technology originally developed by FingerWorks.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to grep for a word and display last transection?

Hi, When we "grep" for a word in a file, it returns the last lines containing the word that we searched for. Is there a way to display last line to grep. Thanks Ex log. Ex. logname.log 2015-07-29 06:43:07.023|BETA |2015-07-29... (5 Replies)
Discussion started by: ooilinlove
5 Replies

2. Shell Programming and Scripting

Multi line document to single lines based on occurance of string

Hi Guys, I am new to awk and sed, i am working multiline document, i want to make make that document into SINGLE lines based on occurace of string "dwh". here's the sample of my problem.. dwh123 2563 4562 4236 1236 78956 12394 4552 dwh192 2656 46536 231326 65652 6565 23262 16625623... (5 Replies)
Discussion started by: victor369
5 Replies

3. Shell Programming and Scripting

Search a String and display only word.

Hello Gurus, Apologies if this Q has been repeated but i was not able to find it :( I have an input file: ------------------------------- Replace DB.Employee as select column1 column2 from DB_T.Emp and DB.Test and DB.Dept and DB_T.Ter; ------------------------ (4 Replies)
Discussion started by: indrajit_u
4 Replies

4. Shell Programming and Scripting

Parse a file to display lines containing a word

Hi! I'm trying to create a shell script to parse a file which might have multiple lines matching a pattern (i.e. containing some word). I need to return all lines matching the pattern, but stripping the contents of that line until the pattern is matched For example, if my input file was ... (4 Replies)
Discussion started by: orno
4 Replies

5. UNIX for Dummies Questions & Answers

how to grep the word and display only the second word from it

hi, consider the below line in a text file, 'Y',getdate(),'N','V',NULL ..... 'N',getdate(),'Y','D',NULL ..... 'Y','N','Y',getdate(),'Y','D',NULL .... as u see above, i want only the second word after the getdate() word... getdate() will not come 2nd word alwys it may be any position but i... (11 Replies)
Discussion started by: prsam
11 Replies

6. Shell Programming and Scripting

grep display word only

Folks, is it possible to display only words with grep (or any built-in ultility)? I have more than 1 pattern to search, say apple & orange The text goes like this: So I need to display all the words starting with apple or orange The output should be: Any idea? (7 Replies)
Discussion started by: bsddaemon
7 Replies

7. Shell Programming and Scripting

How can I match lines with just one occurance of a string in awk?

Hi, I'm trying to match records using awk which contain only one occurance of my string, I know how to match one or more (+) but matching only one is eluding me without developing some convoluted bit of code. I was hoping there would be some simple pattern matching thing similar to '+' but... (9 Replies)
Discussion started by: jonathanm
9 Replies

8. Shell Programming and Scripting

How to Grep for particular word and display..

Hi Guru's.... I've one log file in all my systems which writes the backup information.. I'have written a command like this: ssh -l ora${sid} ${primaryhost} "tail -50 /oracle/$ORACLE_SID/newbackup/END_BACKUP.log" |grep 'insert' |tail -1| awk '{print $7}' We have nearly 50 systems in our... (2 Replies)
Discussion started by: suri.tyson
2 Replies

9. UNIX for Dummies Questions & Answers

how to grep for a word and display only the word

Hi, When we "grep" for a word in a file, it returns the lines containing the word that we searched for. Is there a way to display only the words and not the entire line containing them. Thanks Ananth (6 Replies)
Discussion started by: ananthmm
6 Replies

10. UNIX for Dummies Questions & Answers

grep a word and display its column

Hi, I need idea about this, say I have this line: 05 21 * * 0,6 /user/clean.desktop.sh > /tmp/desktop_rpt 2>&1 I would need to grep the word desktop and display the /user/clean.desktop.sh and not the whole line. And if I have some more lines say, 05 21 * * 0,6 /user/clean.desktop.sh >... (1 Reply)
Discussion started by: Orbix
1 Replies

Featured Tech Videos