shell script: grep multiple lines after pattern match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script: grep multiple lines after pattern match
Prev   Next
# 1  
Old 08-10-2010
Network shell script: grep multiple lines after pattern match

I have sql file containing lot of queries on different database table. I have to filter specific table queries.
Let say i need all queries of test1,test2,test3 along with four lines above it and sql queries can be multi lines or in single line.

Input file contains.

Code:
set INSERT_ID=1;
set TIMESTAMP=20100808
# at 233
# sql.session.value

INSERT INTO test1 (
column1,
column2,
column3,

VALUES

(
column1value,
column2value,
column3value
)

set INSERT_ID=21;
set TIMESTAMP=20100809
# at 2221
# sql.session.value

INSERT INTO notrequired (
column1

VALUES

(
column1value
)

set INSERT_ID=2;
set TIMESTAMP=20100809
# at 233
# sql.session.value

INSERT INTO test3 column1,column2 VALUES (column1value,column2value)

set INSERT_ID=2;
set TIMESTAMP=20100809
# at 233
# sql.session.value

UPDATE test1
SET column1=value,
column2=value,
column3=value
WHERE column3=value;

set INSERT_ID=11;
set TIMESTAMP=20100808
# at 2343
# sql.session.value

INSERT INTO test2 (
column1,
column2

VALUES

(
column1value,
column2value
)


I need to get all test1 and test2 table queries along with four above lines. I tried following but didn't work. Only grep first line of table.

shell] cat inputfile.sql | grep -wB4 "INSERT INTO test1" > outputfile.sql
shell] cat inputfile.sql | grep -wB4 "UPDATE test1" >> outputfile.sql
shell] cat inputfile.sql | grep -wB4 "INSERT INTO test2" >> outputfile.sql
shell] cat inputfile.sql | grep -wB4 "INSERT INTO test3" >> outputfile.sql

Output file should be look likes this,

Code:
set INSERT_ID=1;
set TIMESTAMP=20100808
# at 233
# sql.session.value

INSERT INTO test1 (
column1,
column2,
column3,

VALUES

(
column1value,
column2value,
column3value
)


set INSERT_ID=2;
set TIMESTAMP=20100809
# at 233
# sql.session.value

INSERT INTO test3 column1,column2 VALUES (column1value,column2value)


set INSERT_ID=2;
set TIMESTAMP=20100809
# at 233
# sql.session.value

UPDATE test1
SET column1=value,
column2=value,
column3=value
WHERE column3=value;

set INSERT_ID=11;
set TIMESTAMP=20100808
# at 2343
# sql.session.value

INSERT INTO test2 (
column1,
column2

VALUES

(
column1value,
column2value
)

Any help highly appreciated.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

2. Shell Programming and Scripting

Remove multiple lines that match pattern

Not sure how I can accomplish this. I would like to remove all interfaces that have the commands I would like to see: switchport port-security, spanning-tree portfast. One line is no problem. interface FastEthernet0/8 spanning-tree portfast interface FastEthernet0/9 spanning-tree... (4 Replies)
Discussion started by: mrlayance
4 Replies

3. 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

4. Shell Programming and Scripting

Grep multiple exact match, do not display lines

Hi, Need help to grep the following from a file x. I just want to grep exact match not lines and not partial word. CONFSUCCESS CONFFAIL CONFPARTIALSUCCESS >cat x xczxczxczc zczczcxx CONFSUCCESS czczczcczc czxxczxzxczcczc CONFFAIL xczxczcxcczczc zczczczcz CONFPARTIALSUCCESS czczxcxzc ... (4 Replies)
Discussion started by: rajeshwebspere
4 Replies

5. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

6. Shell Programming and Scripting

how to convert a shell script to a php script for displaying next word after pattern match

I have a shell script which I made with the help of this forum #!/bin/sh RuleNum=$1 cat bw_rules | sed 's/^.*-x //' | awk -v var=$RuleNum '$1==var {for(i=1;i<=NF;i++) {if($i=="-bwout") print $(i+3),$(i+1)}}' Basically I have a pages after pages of bandwidth rules and the script gives... (0 Replies)
Discussion started by: sb245
0 Replies

7. Shell Programming and Scripting

Grep multiple line pattern and output the lines

Hi I have the following Input -- -- TABLE: BUSINESS_UNIT -- ALTER TABLE RATINGS.BUSINESS_UNIT ADD CONSTRAINT FK1_BUSINESS_UNIT FOREIGN KEY (PEOPLESOFT_CHART_FIELD_VALUE_ID) REFERENCES RATINGS.PEOPLESOFT_CHART_FIELD_VALUE(PEOPLESOFT_CHART_FIELD_VALUE_ID) ; ALTER TABLE... (1 Reply)
Discussion started by: pukars4u
1 Replies

8. Shell Programming and Scripting

Perl: Printing Multiple Lines after pattern match

Hello People, Need some assistance/guidance. OUTLINE: Two files (File1 and File2) File1 has some ids such as 009463_3922_1827 897654_8764_5432 File2 has things along the lines of: Query= 009463_3922_1827 length=252 (252 letters) More stufff here ... (5 Replies)
Discussion started by: Deep9000
5 Replies

9. Shell Programming and Scripting

Perl script to match a pattern and print lines

Hi I have a file (say 'file1')and I want to search for a first occurence of pattern (say 'ERROR') and print ten lines in the file below pattern. I have to code it in PERL and I am using Solaris 5.9. I appreciate any help with code Thanks Ammu (6 Replies)
Discussion started by: ammu
6 Replies

10. Shell Programming and Scripting

Concatenating multiple lines to one line if match pattern

Hi all, I've been working on a script which I have hit a road block now. I have written a script using sed to extract the below data and pumped into another file: Severity............: MAJORWARNING Summary: System temperature is out of normal range. Severity............: MAJORWARNING... (13 Replies)
Discussion started by: phixsius
13 Replies
Login or Register to Ask a Question