Sponsored Content
Top Forums Shell Programming and Scripting shell script: grep multiple lines after pattern match Post 302444005 by mirfan on Tuesday 10th of August 2010 04:11:11 PM
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.
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
cvm-mysql(8)						      System Manager's Manual						      cvm-mysql(8)

NAME
cvm-mysql - MySQL module SYNOPSIS
cvm-mysql CREDENTIALS
Pass phrase DESCRIPTION
This module queries a MySQL database for the account name, compares the stored pass phrase with the given one using crypt(3). CONFIGURATION VARIABLES
CVM_MYSQL_DEFAULT_FILE The full path of the defaults file to read if the following variable is set. If not set, the file $HOME/.my.cnf will be read (the MySQL default). CVM_MYSQL_DEFAULT_GROUP If set, the module will read connection default options from the named group in the defaults file as above CVM_MYSQL_HOST The hostname or IP of the MySQL server. If not set, a connection to the local host is assumed. CVM_MYSQL_USER The MySQL login ID to connect as. If not set, the invoking user is assumed. CVM_MYSQL_PASS The password for the above user. CVM_MYSQL_DB The database name, must be set. CVM_MYSQL_PORT The port number for the TCP/IP connection (only used if the server is not local). CVM_MYSQL_POSTQ (optional) The SQL query to execute after the credentials have been validated, see cvm-sql(7). CVM_MYSQL_PWCMP (optional) The password comparison module to use. CVM_MYSQL_SOCKET The path to the socket that should be used for connections to a local server. CVM_MYSQL_QUERY (optional) The SQL query to issue to retrieve the row containing the account information from the database, see cvm-sql(7). SEE ALSO
cvm-sql(7), cvm-pgsql(8), cvm-pwfile(8), cvm-qmail(8), cvm-unix(8), cvm-vmailmgr(8), cvm-benchclient(8), cvm-checkpassword(8), cvm-test- client(8) http://untroubled.org/pwcmp/pwcmp.html http://untroubled.org/cvm/cvm.html cvm-mysql(8)
All times are GMT -4. The time now is 01:32 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy