print lines between 2 matching patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print lines between 2 matching patterns
# 1  
Old 08-08-2011
print lines between 2 matching patterns

Hi Guys,

I have file like below, I want to print all lines between test1231233 to its 10 occurrence(till line 41)
Code:
test1231233
  qwe
  qwe
  qweq123 
test1231233
  qwe
  qwe
  qweq23 
test1231233
  qwe
  qwe
  qweq123 
test1231233
  qwe
  qwe
  qweq123131
test1231233
  qwe
  qwe
  qweq123sd
test1231233
  qwe
  qwe
  qweq123a 
test1231233
  qwe
  qwe
  qweq123 
test1231233
  qwe
  qwe
  qweq123131
test1231233
  qwe
  qwe
  qweq12313 
test1231233
  qwe
  qwe
  qweq21 
test1231233
  qwe
  qwe
  qweq12 
test1231233
  qwe
  qwe
  qweq2 
test1231233
  qwe
  qwe
  qweq1 
test1231233
  qwe
  qwe
  qweq

Tried sed '/test1231233/,/test1231233/' FILENAME but this prints almost everything excluding last three lines.

Appreciate your help......

Last edited by Scott; 08-08-2011 at 10:18 AM.. Reason: Code tags, please...
# 2  
Old 08-08-2011
Code:
awk '/test1231233/ && x!=11 {print; x++; next} x<11' infile

# 3  
Old 08-08-2011
Alternate awk..
Code:
awk '/test1231233/{i++}(i==11){print $0;exit}i' inputfile

# 4  
Old 08-08-2011
print lines between 2 matching patterns

thanks a lot guys .... both the reply worked for me ...now tring to understand it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. Shell Programming and Scripting

How to print different multiple lines after two patterns?

Hello, I need to print some lines as explained below, TXT example 1111 2222 3333 4444 5555 6666 7777 8888 6666 9999 1111 2222 3333 4444 5555 (8 Replies)
Discussion started by: liuzhencc
8 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. UNIX for Dummies Questions & Answers

Matching two patterns in the consecutive lines

Hi Experts I need to match 2 patterns consecutively and display 25 lines after that. 1st one - Error 2nd one - End string ( comes along with the pattern one) 3rd one - error Logic grep "ERROR OCCURRED :" trace.log | awk -v "ES=:" -v "SS=java.lang.NullPointerException" '{ if($NF ~... (8 Replies)
Discussion started by: senthil.ak
8 Replies

5. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

6. Shell Programming and Scripting

Perl : to print the lines between two patterns

Hello experts, I have a text file from which I need to print all the lines between the patterns. Could anyone please help me with the perl script. names.txt ========= Badger Bald Eagle Bandicoot Bangle Tiger Barnacle Barracuda Basilisk Bass Basset Hound Beetle Beluga... (7 Replies)
Discussion started by: scriptscript
7 Replies

7. Shell Programming and Scripting

Print all lines between patterns

Hi Gurus, I have a requirement where I need to display all lines between 2 patterns except the line where the first pattern in it. I tried the following command using awk but it is printing all lines except the lines where the 2 patterns exist. awk '/TRANSF_/{ P=1; next } /Busy/ {exit} P'... (9 Replies)
Discussion started by: svajhala
9 Replies

8. Shell Programming and Scripting

How to print only lines in between patterns?

Hi, I want to print only lines (green-italic lines) in between first and last strings in column 9. there are different number of lines between each strings. 10 AUGUSTUS exon 4558 4669 . - . 10.g1 10 AUGUSTUS exon 8771 8889 . ... (6 Replies)
Discussion started by: jamo
6 Replies

9. Shell Programming and Scripting

Need to print between patterns AND a few lines before

I need to print out sections (varying numbers of lines) of a file between patterns. That alone is easy enough: sed -n '/START/,/STOP/' I also need the 3 lines BEFORE the start pattern. That alone is easy enough: grep -B3 START But I can't seem to combine the two so that I get everything between the... (2 Replies)
Discussion started by: Finja
2 Replies

10. Shell Programming and Scripting

Print lines between two repetitive patterns

Hi users I have one file which has number of occurrence of one pattern examples Adjustmenttype,11 xyz 10 dwe 9 abd 13 def 14 Adjustmenttype,11 xyz 24 dwe 34 abd 35 def 11 nmb 12 Adjustmenttype, not eleven .... ... ... (2 Replies)
Discussion started by: eranmoh
2 Replies
Login or Register to Ask a Question