Conditional Multi-Line Grep Problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional Multi-Line Grep Problem
# 1  
Old 08-02-2011
Conditional Multi-Line Grep Problem

Hi, I have a very large file I want to extract lines from. I'm hoping Grep can do the job, but I'm running into problems.

I want to return all lines that match a pattern. However, if the following line of a matched line contains the word "Raw" I want to return that line as well.

Is this doable?

For example,

Input:
Code:
pattern blah blah blah
something nothing
pattern something ready
nothing Raw

A grep on "pattern" should return:
Code:
pattern blah blah blah
pattern something ready
nothing Raw

Because the line "pattern something ready" is followed by a line containing "Raw", "nothing Raw" is also included.

Thanks

Last edited by radoulov; 08-02-2011 at 03:47 PM.. Reason: Code tags!
# 2  
Old 08-02-2011
Please post a sample of your input and an example of the expected output.
# 3  
Old 08-02-2011
Is this what you want:
Code:
sed -n -e '/pattern/p' -e '/pattern/{n;/Raw/p;}' Input_File

This User Gave Thanks to Shell_Life For This Post:
# 4  
Old 08-02-2011
Quote:
Originally Posted by Shell_Life
Is this what you want:
Code:
sed -n -e '/pattern/p' -e '/pattern/{n;/Raw/p;}' Input_File

That works. As an aside, how would I edit that command to handle the situation where there is a blank line between each line in the input file? Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep multi line with keyword ?

Hello Everyone, i need to grep specific keyword in a file. i want need solution to output. example file.txt 03-08-2019 21:02:20,938 ::: Recieve Data From Amazon ::: 03-08-2019 21:02:20,938 IP : 192.168.1.1 | msg = Your confirmation code for 'Verify phone number' is xxxxx | sno =... (2 Replies)
Discussion started by: ooilinlove
2 Replies

2. Shell Programming and Scripting

Help with reformat single-line multi-fasta into multi-line multi-fasta

Input File: >Seq1 ASDADAFASFASFADGSDGFSDFSDFSDFSDFSDFSDFSDFSDFSDFSDFSD >Seq2 SDASDAQEQWEQeqAdfaasd >Seq3 ASDSALGHIUDFJANCAGPATHLACJHPAUTYNJKG ...... Desired Output File >Seq1 ASDADAFASF ASFADGSDGF SDFSDFSDFS DFSDFSDFSD FSDFSDFSDF SD >Seq2 (4 Replies)
Discussion started by: patrick87
4 Replies

3. Shell Programming and Scripting

Multi-conditional IF - awk

I have a 6 column array with 2 million rows that looks like this: 1 1089699 rs6686003 G A g 1 1090557 rs7553429 A C c 1 1094738 rs4970362 A G a 1 1099342 rs9660710 A C c 1 1106473 rs4970420 G A a 1 1108637 rs4970421 G A g 1 1119858 rs1320565 C T c 1... (5 Replies)
Discussion started by: Geneanalyst
5 Replies

4. Shell Programming and Scripting

How to grep multi line with keyword ?

Hi All. how to grep with same keyword. 23-07-2012 15:15:30,117 ::: Recieve Message From Commadu ::: .. ... .... 23-07-2012 16:15:28,481 ::: Recieve Message From Commadu ::: 23-07-2012 16:15:28,481 IP : 127.0.0.1 | msg =... (1 Reply)
Discussion started by: ooilinlove
1 Replies

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

6. UNIX for Dummies Questions & Answers

find/xargs/*grep: find multi-line empty "try-catch" blocks - eg, missing ; not in a commented block

How can I recursively find all files in a directory and print out the file and first line number of any text blocks that match the below cases? This would seem to involve find, xargs, *grep, regex, etc. In summary, I want to find so-called empty "try-catch blocks" that do not contain code... (0 Replies)
Discussion started by: lifechamp
0 Replies

7. Shell Programming and Scripting

if statement with grep as conditional

Please see the script segment below for i in $files do echo $i if ; then case "$1" in "IE0263"|"IE0264"|"IE0267"|"IE0268") short_filename=`ls -l $i | cut -c108-136 | sort` ;; "IE0272"|"IE0273") short_filename=`ls -l $i | cut... (4 Replies)
Discussion started by: jmahal
4 Replies

8. Shell Programming and Scripting

How to grep multi line.

How to grep multi line. My LOg. . . . . 2010-04-23 02:17:02,419 INFO - -MsgCode = 00903 2010-04-23 02:17:02,420 INFO - - end processABCD126 2010-04-23 02:17:02,420 DEBUG - try to get message=ERROR_NOT_AUTHORIZED_TO_USE_SERVICE 2010-04-23 02:17:02,420 DEBUG - got message=Test... (4 Replies)
Discussion started by: ooilinlove
4 Replies

9. Shell Programming and Scripting

Problem while using grep (multi-level) with the space-separated filepath.

Hi, I've been trying to use grep to find out all the files which have two particular patterns in it (both pattern1 AND pattern2). I have a script to do the same, in which I'm getting the output of the first grep (with -l option) which contains the list of file paths and I'm trying to search for... (3 Replies)
Discussion started by: NanJ
3 Replies

10. Shell Programming and Scripting

AWK Multi-Line Records Numbering Problem

I have a set of files of multi-line records with the records separated by a blank line. I needed to add a record number to the front of each line followed by a colon and did the following: awk 'BEGIN {FS = "\n"; RS = ""}{for (i=1; i<=NF; i++)print NR,":",$i}' ~/Desktop/data98-1-25.txt >... (3 Replies)
Discussion started by: RacerX
3 Replies
Login or Register to Ask a Question