Print all lines before first match | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Print all lines before first match

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-30-2012
lasserfox lasserfox is offline
Registered User
 
Join Date: Nov 2012
Last Activity: 30 November 2012, 5:08 AM EST
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Print all lines before first match

Hi, I have this file.

Code:
close
block3c
block3b
block3a
open
close
block2b
block2a
open
close
block1a
open

and I need :

Code:
open
block3a
block3b
block3c
close

I think that it could be done with sed, but does anyone can help me with that?
Thanks

Last edited by Franklin52; 11-30-2012 at 05:46 AM.. Reason: Please use code tags for data and code samples
Sponsored Links
    #2  
Old 11-30-2012
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 14 April 2014, 6:10 AM EDT
Posts: 1,640
Thanks: 58
Thanked 476 Times in 472 Posts
Please use code tags for code and data sample.


Code:
 awk '/open/ && !a{a=NR}{A[NR]=$0}END{for(i=a;i>=1;i--){print A[i]}}' file

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Print lines that match certain criteria gautig Shell Programming and Scripting 2 03-14-2012 01:04 PM
Print lines before and after pattern match manuswami Shell Programming and Scripting 7 02-01-2012 06:07 AM
find a word and print n lines before and after the match chidori Shell Programming and Scripting 14 12-09-2011 02:58 PM
Awk print all lines on match? MrEddy UNIX for Dummies Questions & Answers 9 04-08-2011 10:55 AM
how to print all lines from a second match kchinnam Shell Programming and Scripting 1 08-11-2010 04:22 PM



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