Grep multiple line pattern and output the lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep multiple line pattern and output the lines
# 1  
Old 11-11-2009
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 RATINGS.CASE ADD CONSTRAINT FK7_CASE
FOREIGN KEY (ENTITY_ID, VERSION_NBR)
REFERENCES RATINGS.ENTITY_VERSION(ENTITY_ID, VERSION_NBR)
;



ALTER TABLE RATINGS.CANADIAN_PROVINCE ADD CONSTRAINT FK1_CANADIAN_PROVINCE
FOREIGN KEY (ENTITY_ID, VERSION_NBR)
REFERENCES RATINGS.ENTITY_VERSION(ENTITY_ID, VERSION_NBR)
;

--
-- TABLE: CANADIAN_TERRITORY
--

ALTER TABLE RATINGS.CANADIAN_TERRITORY ADD CONSTRAINT FK1_CANADIAN_TERRITORY
FOREIGN KEY (ENTITY_ID, VERSION_NBR)
REFERENCES RATINGS.ENTITY_VERSION(ENTITY_ID, VERSION_NBR)
;

I need to get ouput of alter staments where in it

REFERENCES RATINGS.ENTITY_VERSION


Output looks like


ALTER TABLE RATINGS.CANADIAN_TERRITORY ADD CONSTRAINT FK1_CANADIAN_TERRITORY
FOREIGN KEY (ENTITY_ID, VERSION_NBR)
REFERENCES RATINGS.ENTITY_VERSION(ENTITY_ID, VERSION_NBR)
;
# 2  
Old 11-11-2009
Code:
cat abc.txt
--
-- 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)
;

....

Code:
 cat abc.txt | perl -e 'while(<>){
        chomp;
        next if (m/--/);
        $str .= $_;
        if ($_ =~ m/;/){
                print "$str\n" if ($str =~ m/REFERENCES RATINGS.ENTITY_VERSION/);
                $str = "";
        }
}'

Replace abc.txt with the file you have.

HTH,
PL
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

Group Multiple Lines on SINGLE line matching pattern

Hi Guys, I am trying to format my csv file. When I spool the file using sqlplus the single row output is wrapped on three lines. Somehow I managed to format that file and finally i am trying to make the multiple line on single line. The below command is working fine but I need to pass the... (3 Replies)
Discussion started by: RJSKR28
3 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

Multiple pattern match and print the output in a single line

I need to match two patterns in a log file and need to get the next line of the one of the pattern (out of two patterns) that is matched, finally need to print these three values in a single line. Sample Log: 2013/06/11 14:29:04 <0999> (725102) Processing batch 02_1231324 2013/06/11... (4 Replies)
Discussion started by: rpm120
4 Replies

5. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

6. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

7. Shell Programming and Scripting

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. set INSERT_ID=1; set... (1 Reply)
Discussion started by: mirfan
1 Replies

8. Solaris

Multiple pattern on same line using grep

Hi, I would like to search multiple patterns on same line, i.e. all patterns must present on same line. Please suggest. Thanx (2 Replies)
Discussion started by: sanjay1979
2 Replies

9. Shell Programming and Scripting

using tr to put multiple lines of output into one line

Hi all, For a intro UNIX course I'm taking, I need to use the command "tr" to display a file on standard output without any newlines (all on one line). I assume I would start with "cat filename | tr" but don't know what to put after tr. Any ideas would be lovely! Thanks. (3 Replies)
Discussion started by: otes4
3 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