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
How to print only lines in between two strings using awk jisha Shell Programming and Scripting 4 01-11-2008 04:13 AM
Print to ps2pdf print queue Sean_69 SUN Solaris 2 10-22-2007 12:00 PM
Print Problem in UNIX. Need to know the option to specify the print paper size ukarthik HP-UX 1 06-07-2007 10:35 AM
How do I get awk to print a " in it's print part? LordJezo Shell Programming and Scripting 2 06-27-2006 10:16 PM
How to concatenate two strings or several strings into one string in B-shell? fontana Shell Programming and Scripting 2 08-26-2005 12:58 PM

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 01-08-2009
soots soots is offline
Registered User
  
 

Join Date: Aug 2005
Location: Loch Lomond
Posts: 6
Print all between 2 strings

Hi All,

I'm working on a large file and need to extract all data between 2 strings. I have seen many good solutions to threads almost like my problem but none that quite fit.
This is all very new to me so any ideas would be really appreciated! (attempted to read sed and awk tutorials but got a headache trying to take it all in!)

Sample of data...

DEF on
DEF off
ABC on
DEF on
DEF off
GHI on
DEF on
ABC off
ABC on
ABC off
DEF off
etc..etc

There are duplications of the main strings I need to include in the output file, so I need to capture the first instance 'ABC on' and the last instance of 'ABC off'.
I need to extract all the data to resemble the output below..

ABC on (first occurence)
DEF on
DEF off
GHI on
DEF on
ABC off
ABC on
ABC off (last occurence)

Thanks In Advance.
Soots
  #2 (permalink)  
Old 01-08-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,312
One way to give you the desired output:

Code:
awk 'NR==FNR {if($0=="ABC off"){r=NR};next}
/ABC on/{f=1}
FNR==r{print;exit}f' file file
Regards
  #3 (permalink)  
Old 01-08-2009
soots soots is offline
Registered User
  
 

Join Date: Aug 2005
Location: Loch Lomond
Posts: 6
Thanks for the response...for some reason I get the following error messages:

awk: syntax error near line1
awk: bailing out near line 1

I've check the syntax of your script on my machine, it is as per your posting. Any thoughts appreciated.

Regards

Soots
  #4 (permalink)  
Old 01-08-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,312
Quote:
Originally Posted by soots View Post
Thanks for the response...for some reason I get the following error messages:

awk: syntax error near line1
awk: bailing out near line 1

I've check the syntax of your script on my machine, it is as per your posting. Any thoughts appreciated.

Regards

Soots
Use nawk or /usr/xpg4/bin/awk on Solaris.

Regards
  #5 (permalink)  
Old 01-08-2009
soots soots is offline
Registered User
  
 

Join Date: Aug 2005
Location: Loch Lomond
Posts: 6
Did as suggested, used /usr/xpg4/bin/awk. Error messages went away but output did not materialise. The strings I'm using as begin and end placeholders are within a .dat file called machinelog.dat that looks similar to this

6/1/09 23:22:00 Machine1 Initialising : Initialising
6/1/09 23:22:01 Machine1 DEF on : system on
6/1/09 23:22:04 Machine1 ABC on : system on
6/1/09 23:22:05 Machine1 DEF on : system on
6/1/09 23:22:06 Machine1 ABC off : system off
6/1/09 23:22:07 Machine1 DEF off : system off
6/1/09 23:22:22 Machine1 ABC on : system on
6/1/09 23:22:25 Machine1 ABC off : system off
6/1/09 23:22:38 Machine1 ABC on : system on
6/1/09 23:23:07 Machine1 DEF on : system on
6/1/09 23:23:09 Machine1 ABC on : system on
6/1/09 23:23:15 Machine1 DEF on : system on
6/1/09 23:23:16 Machine1 ABC off : system failure
6/1/09 23:23:18 Machine1 DEF off : system off
6/1/09 23:23:22 Machine1 ABC on : system on
6/1/09 23:23:25 Machine1 ABC off : system failure
6/1/09 23:23:38 Machine1 ABC on : system on
6/1/09 23:24:07 Machine1 DEF on : system on

and I was hoping to capture everything between the first ABC on and the last system failure and put it into a new file called machinelogmain.dat, like below:

6/1/09 23:22:04 Machine1 ABC on : system on
6/1/09 23:22:05 Machine1 DEF on : system on
6/1/09 23:22:06 Machine1 ABC off : system off
6/1/09 23:22:07 Machine1 DEF off : system off
6/1/09 23:23:22 Machine1 ABC on : system on
6/1/09 23:23:25 Machine1 ABC off : system off
6/1/09 23:23:38 Machine1 ABC on : system on
6/1/09 23:22:07 Machine1 DEF on : system on
6/1/09 23:22:00 Machine1 ABC on : system on
6/1/09 23:22:01 Machine1 DEF on : system on
6/1/09 23:22:04 Machine1 ABC off : system failure
6/1/09 23:22:07 Machine1 DEF off : system off
6/1/09 23:23:22 Machine1 ABC on : system on
6/1/09 23:23:25 Machine1 ABC off : system failure

I tried to substitute the ABC on/ABC off etc but not much joy! Does your script format still apply or will it require a change?

Thanks for your patience.

Regards soots
  #6 (permalink)  
Old 01-09-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,312
Try this

Code:
awk 'NR==FNR {if($0 ~ "system failure"){r=NR};next}
/ABC on/{f=1}
FNR==r{print;exit}f' file file
Regards
Closed Thread

Bookmarks

Tags
save data between two strings

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 10:33 PM.


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