The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Getting the lines between last occurrence of two patterns rvz Shell Programming and Scripting 8 08-08-2008 05:32 PM
get the value between 2 patterns minifish Shell Programming and Scripting 11 04-07-2008 02:18 PM
how to grep for lines between 2 give patterns? melanie_pfefer Shell Programming and Scripting 1 04-03-2008 11:38 AM
How to count lines - ignoring blank lines and commented lines kthatch UNIX for Dummies Questions & Answers 6 05-25-2007 01:21 AM
search patterns rochitsharma Shell Programming and Scripting 1 02-27-2006 04:14 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-23-2008
racbern racbern is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 26
How to get lines in between Patterns?

Hi,

I need to create a script that does the following:

1. Read the file for the occurrences of "EXECUTE" and "END" strings.
There will be several occurrences of EXECUTE and END strings on the file.
2. The resulting lines in #1, needs to be searched for the word "Error|failed|error|warning", if the word error occurs on the line, prints the specific line and 1 line above it.

Here's a sample input file:
HTML Code:
-------------------------------< EXECUTE >----------------------------
This session consists of:  Feature  R-state        Install-type Upgrade-from

------------------------------------------------------------------------  
conf_system                              R3C01          upgrade      R3B03     
------------------------------------------------------------------------
------------------< CIF(conf) 2008-04-08 02:31:28 >--------------------
----------------------< END 2008-04-08 02:31:57 >---------------------

-------------------------------< EXECUTE >-----------------------------
---------< FM(fm_core,fmba,fmaa,fmx,fmav) 2008-04-08 03:14:09 >-------
[main] Error installing feature [fmx] - com.er.nm.if.ist.NoMatchingUpgradeClauseFoundException: 
--------------------------< END 2008-04-08 03:22:51 >------------------

The output should be:
HTML Code:
---------< FM(fm_core,fmba,fmaa,fmx,fmav) 2008-04-08 03:14:09 >-------
[main] Error installing feature [fmx] - com.er.nm.if.ist.NoMatchingUpgradeClauseFoundException: 

I'm not familiar with awk command, and if you can guide me on this please?
Any help is greatly appreciated.

//racbern
  #2 (permalink)  
Old 04-23-2008
aju_kup aju_kup is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 189
try this

#! /usr/bin/ksh

cat linebetween.d | awk '
/EXECUTE/ { start = 1; nextline=1; continue }
/END/ { start = 0 ; continue}
(/Error/ || /failed/ || /error/ || /warning/) && start == 1 { print hesderline; print $0 }
{ if (nextline == 1 ) {hesderline = $0; nextline=0}}
'
  #3 (permalink)  
Old 04-23-2008
racbern racbern is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 26
Hi aju_kup,

Thanks for your quick reply.

I tried your suggestion, and it only prints the lines with the patterns "Error|failed|error|warning". I need to get also the line above it.


I did some trial and error of the awk command and come out with the following (but I dont know how to get the line above it):

# awk '/EXECUTE/,/END/ { if ($0 ~ /Error installing/) print $0}' /var/tmp/llsva.log


//racbern
  #4 (permalink)  
Old 04-23-2008
aju_kup aju_kup is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 189
#! /usr/bin/ksh

cat linebetween.d | awk '
/EXECUTE/ { start = 1; continue }
/END/ { start = 0 ; continue}
(/Error/ || /failed/ || /error/ || /warning/) && start == 1 { print pline; print $0 }
{ pline = $0}
'
  #5 (permalink)  
Old 04-23-2008
racbern racbern is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 26
Hi aju kup,

Thanks for your quick reply.

Can you explain me the code as well please?


//racbern
  #6 (permalink)  
Old 04-23-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
The cat is useless, of course, and you can combine all those search expressions into one big ole regular expression:

Code:
awk '/EXECUTE/ { start = 1; continue }
/END/ { start = 0; continue }
start && /[Ee]rror|failed|warning/ { print pline; print }
{ pline = $0 }' file
The essential point is that pline captures the previous line when it wasn't printed; and start is true when you have seen an EXECUTE but not yet an END. So when start is true and the regular expression matches, you print the previous line and the current line.
  #7 (permalink)  
Old 04-23-2008
racbern racbern is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 26
Hi era,

Thanks for reply as well.
I added == 1 on your suggestion, and it worked fine as well.

awk '/EXECUTE/ { start = 1; continue }
/END/ { start = 0; continue }
start == 1 && /[Ee]rror|failed|warning/ { print pline; print }
{ pline = $0 }' file

Is there a way I can assign the pline to a variable, so that I can manipulate it afterwards?

Thanks again..

//racbern
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:40 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0