The UNIX and Linux Forums  

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 here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
c program to extract text between two delimiters from some text file kukretiabhi13 High Level Programming 7 1 Day Ago 03:29 PM
Using awk to extract text Davizzle Shell Programming and Scripting 5 08-14-2008 10:09 PM
replacing strings with text from other file mc1392 Shell Programming and Scripting 1 03-24-2008 12:46 PM
extracting a set of strings from a text file Deanne Shell Programming and Scripting 2 09-20-2007 08:31 PM
How to extract text from xml file chrisf Shell Programming and Scripting 3 09-01-2007 11:25 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 02-04-2008
Registered User
 

Join Date: Feb 2008
Posts: 7
using awk to extract text between two constant strings

Hi,

I have a file from which i need to extract data between two constant strings.
The data looks like this :

Line 1 SUN> read db @cmpd unit 60
Line 2 Parameter: CMPD -> "C00071"
Line 3
Line 4 SUN> generate
Line 5 tabint>ERROR: (Variable data)


The data i need to extract is Line 2 but only when Line 1 is "SUN> read db @cmpd unit 60"
and Line 5 is "tabint>ERROR:"

The following cant print Line 2, moreover i am specific that my start and end string belong to line i and i+4.

awk '/SUN> read db @cmpd unit 60/,/tabint>ERROR/ {print}' database_energy.out > tf4

Any help will be greatly appreciated.

-Manali
Reply With Quote
Forum Sponsor
  #2  
Old 02-04-2008
Registered User
 

Join Date: Feb 2008
Posts: 7
In other words what i want to do is the following:

if row number "i" = /SUN> read db @cmpd unit 60/
and row number "i+4" = /tabint>ERROR:/
print row number "i+1"
Reply With Quote
  #3  
Old 02-04-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
IF the file REALLY has "Line 1", etc. in it:
Code:
awk ' arr[$1 $2]=$0
       if(arr[$1 $2]=="Line5"  &&
          arr["Line1"]=="SUN> read db @cmpd unit 60" &&              
          arr["Line5"]=="tabint>ERROR:")
          {
              print arr["Line2"]
           }
         ' inputfilename
The arr[$1 $2] concatenates the two fields, "Line 1" becomes "Line1"
Reply With Quote
  #4  
Old 02-04-2008
Registered User
 

Join Date: Feb 2008
Posts: 7
I'm sorry i wrote those Line numbers to explain what i wanted to do.
What i would like to do is

if row number "i" = /SUN> read db @cmpd unit 60/
and row number "i+4" = /tabint>ERROR:/
print row number "i+1"
Reply With Quote
  #5  
Old 02-04-2008
Moderator
 

Join Date: Feb 2007
Posts: 2,329
Try this:

Code:
awk '
/^SUN> read db @cmpd unit 60/ || c {
  c++
  if(c==2) {
    var=$0
  }
  if(c==5 && /^tabint>ERROR:.*/) {
    print var
    c=0
  } 
}' file

Regards
Reply With Quote
  #6  
Old 02-04-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,933
Code:
awk '/SUN> read db @cmpd unit 60/ {c = 3; getline; s = $0 }
!c-- && /tabint>ERROR:/ { print s }' input
Use nawk or /usr/xpg4/bin/awk on Solaris.
Reply With Quote
  #7  
Old 02-04-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 570
Quote:
Originally Posted by mjoshi View Post
Hi,

I have a file from which i need to extract data between two constant strings.
The data looks like this :

Line 1 SUN> read db @cmpd unit 60
Line 2 Parameter: CMPD -> "C00071"
Line 3
Line 4 SUN> generate
Line 5 tabint>ERROR: (Variable data)


The data i need to extract is Line 2 but only when Line 1 is "SUN> read db @cmpd unit 60"
and Line 5 is "tabint>ERROR:"

The following cant print Line 2, moreover i am specific that my start and end string belong to line i and i+4.

awk '/SUN> read db @cmpd unit 60/,/tabint>ERROR/ {print}' database_energy.out > tf4

Any help will be greatly appreciated.

-Manali
Code:
[n]awk '/SUN> read db @cmpd unit 60/,/tabint>ERROR:/ {
     if (last_line == "SUN> read db @cmpd unit 60")
        print 
     last_line = $0
}' file
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:53 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0