Print between multi line pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print between multi line pattern
# 1  
Old 02-27-2012
CPU & Memory Print between multi line pattern

Hi,

I have a file with text like this

Code:
.SET WIDTH 10000 
.SET MAXERROR 1   
insert into new_db  
SELECT 
* 
FROM 
some_db  

;
+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+-
.SET WIDTH 10000
+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+-
.SET MAXERROR 1
+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+-

insert into new_db

SELECT
*
FROM
some_db

;

I have tried
Code:
sed -n '/\.SET MAX.*$/,/;/p' file

But I am getting output as
Code:
.SET MAXERROR 1   
insert into new_db  
SELECT 
* 
FROM 
some_db  

;
.SET MAXERROR 1
+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+-

insert into new_db

SELECT
*
FROM
some_db

;

But I need output as below from the second instance i.ee it has to match the pattern
.SET MAXERROR 1
+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+-


Code:
insert into new_db
 
 SELECT
 *
 FROM
 some_db

;

Thanks in advance

Last edited by sol_nov; 02-27-2012 at 06:33 PM..
# 2  
Old 02-27-2012
Code:
awk '/insert/,/;/' file

# 3  
Old 02-27-2012
CPU & Memory

Quote:
Originally Posted by bartus11
Code:
awk '/insert/,/;/' file

I can use that, but it is not always same, it can have select, delete, create, drop or any thing .
# 4  
Old 02-27-2012
Try:
Code:
perl -ln0e '/\+-\n\n(.*?;)/s;print $1' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. Shell Programming and Scripting

How to find all the multi line pattern and redirecting it to a file?

I've a file like this {multi line ....... ....... pattern} { some other stuff ......... } {multi line ....... ....... pattern} { some other stuff ......... } and so on (2 Replies)
Discussion started by: aamir_raihan
2 Replies

3. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

4. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

5. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

6. Shell Programming and Scripting

Print only next pattern in a line after a pattern match

I have 2013-06-11 23:55:14 1Umexd-0004cm-IG <= user@domain.com I need sed/awk operation on this, so that it should print the very next pattern only after the the pattern mach <= ie only print user@domain.com (7 Replies)
Discussion started by: anil510
7 Replies

7. Shell Programming and Scripting

sed multiple multi line blocks of text containing pattern

Hi, I have a log file which has sessionids in it, each block in the log starts with a date entry, a block may be a single line or multiple lines. I need to sed (or awk) out the lines/blocks with that start with a date and include the session id. The files are large at several Gb. My... (3 Replies)
Discussion started by: andyatit
3 Replies

8. Shell Programming and Scripting

Script to compare pattern and print a different pattern in each line

Hi, I am writing a shell script to parse some files, and gather data. The data in the files is displayed as below. .......xyz: abz: ......qrt: .... .......xyz: abz: ......qrt: ... I have tried using awk and cut, but the position of these values keep changing, so I wasn't able to get... (2 Replies)
Discussion started by: Serena
2 Replies

9. Shell Programming and Scripting

Multi-line filtering based on multi-line pattern in a file

I have a file with data records separated by multiple equals signs, as below. ========== RECORD 1 ========== RECORD 2 DATA LINE ========== RECORD 3 ========== RECORD 4 DATA LINE ========== RECORD 5 DATA LINE ========== I need to filter out all data from this file where the... (2 Replies)
Discussion started by: Finja
2 Replies

10. Shell Programming and Scripting

Print Line if next line Match a pattern

Hi All, Does anyone know how to print 1H1A....... in peal script print line ^1H1A....... if next line equal 5R0RECEIPT.... Thank for help:D Cat st.txt 1H1A-IN-11-5410-0009420|1010047766|dsds|1|N|IN|IN|000000|1||N|<<<line match 5R0RECEIPT| 5R0RECEIPT|... (2 Replies)
Discussion started by: kittiwas
2 Replies
Login or Register to Ask a Question