Print all lines before first match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print all lines before first match
# 1  
Old 11-30-2012
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 06:46 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 11-30-2012
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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match the value & print lines from the match

Hello, I have a file contains two columns. I need to print the lines after “xxx” so i'm trying to match "xxx" & cut the lines after that. I'm trying with the grep & cut command, if there any simple way to extract this please help me. Sample file : name id AAA 123 AAB 124 AAC 125... (4 Replies)
Discussion started by: Shenbaga.d
4 Replies

2. UNIX for Dummies Questions & Answers

awk - (URGENT!) Print lines sort and move lines if match found

URGENT HELP IS NEEDED!! I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. - Also the matching lines are not moving out of File1.txt ... (1 Reply)
Discussion started by: High-T
1 Replies

3. Shell Programming and Scripting

Match 2 different patterns and print the lines

Hi, i have been trying to extract multiple lines based on two different patterns as below:- file1 @jkm|kdo|aas012|192.2.3.1 blablbalablablkabblablabla sjfdsakfjladfjefhaghfagfkafagkjsghfalhfk fhajkhfadjkhfalhflaffajkgfajkghfajkhgfkf jahfjkhflkhalfdhfwearhahfl @jkm|sdf|wud08q|168.2.1.3... (8 Replies)
Discussion started by: redse171
8 Replies

4. Shell Programming and Scripting

Print lines that do not match the pattern

I need to print the lines that do not match a pattern. I tried using grep -v and sed -n '/pattern/!p', but both of them are not working as I am passing the pattern as variable and it can be null some times. Example ........ abcd...... .........abcd...... .........abcd......... (4 Replies)
Discussion started by: sunny1234
4 Replies

5. Shell Programming and Scripting

Print lines that match certain criteria

Hi all I have a text file with the following format: id col1 col2 col3 col4 col5 col6 col7 ... row1 0 0 0 0 0 0 0 row2 0 0 0 0 0 0 0 row3 0 0 0 0 0 0.2 0 row4 0 0 0 0 0 0 0 row5 0 0 0 0 0 0 0 row6 0 0 0 0.1 0 0 0 row7 0 0 0 0 0 0 0 row8 0 0 0 0 0 0 0 row9 0 0 0 0 0 0 0 ... The file... (2 Replies)
Discussion started by: gautig
2 Replies

6. Shell Programming and Scripting

Print lines before and after pattern match

I am using Solaris, I want to print 3 lines before pattern match pattern 5 lines after pattern match Pattern is abcd to be searched in a.txt. Looking for the solution in sed/awk/perl. Thanks .. Input File a.txt: ================= 1 2 3 abcd 4 5 6 7 8 (7 Replies)
Discussion started by: manuswami
7 Replies

7. Shell Programming and Scripting

find a word and print n lines before and after the match

how to find a word and print n lines before and after the match until a blank line is encounterd (14 Replies)
Discussion started by: chidori
14 Replies

8. Shell Programming and Scripting

print lines with exact pattern match

I have in a file domain.com. 1909 IN A 1.22.33.44 domain.com. 1909 IN A 22.33.44.55 ns1.domain.com. 1699 IN A 33.44.55.66 ns2.domain.com. 1806 IN A 77.77.66.66 I need to "grep" or "awk" out the lines starting with domain.com. as follows. domain.com. 1909 IN A 1.22.33.44 domain.com.... (3 Replies)
Discussion started by: anilcliff
3 Replies

9. UNIX for Dummies Questions & Answers

Awk print all lines on match?

Ok so I can use awk to match a pattern and print the whole line with print $0. Is there any way to just tell awk to print every line of output when the pattern matches? I'm having it wait for the word error and then print that entire line. But what I actually need to see is all the following... (9 Replies)
Discussion started by: MrEddy
9 Replies

10. Shell Programming and Scripting

how to print all lines from a second match

I am trying to parse iostat output for io issues.. I want to print all lines including second occurance of 'extended' till EOF(end of file). Can we do that using awk or sed one liners or do we need a script for it? (1 Reply)
Discussion started by: kchinnam
1 Replies
Login or Register to Ask a Question