Grep multiple search terms with context


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep multiple search terms with context
# 1  
Old 04-20-2012
Grep multiple search terms with context

I have a file that is a sort library in the format:

Code:
##def title1

content1
stuff1

content2
stuff2

##enddef
##def title2

etc..


I want to grep def and content and pull some trailing context from content

so the result would look something like:

Code:
##def title1
content2
stuff2
##def title2
content2
 stuff2
etc..

something like:

Code:
$ grep '##def|Content2 -A 1' ./file

where the -A 1 only applies to the second search term.

thoughts?

thanks.


Moderator's Comments:
Mod Comment Welcome to the UNIX and Linux Forums. Please use code tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-20-2012 at 05:48 PM..
# 2  
Old 04-20-2012
Hi, try this:

Code:
awk '/##def/{print; if( getline && getline ) print }' RS= infile

Code:
awk '/##def/ && c=3; --c==0' RS= infile


Last edited by Scrutinizer; 04-20-2012 at 05:58 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use a grep search to search for a specific string within multiple directories?

Lets say I have a massive directory which is filled with other directories all filled with different c++ scripts and I want a listing of all the scripts that contain the string: "this string". Is there a way to use a grep search for that? I tried: grep -lr "this string" * but I do not... (3 Replies)
Discussion started by: Circuits
3 Replies

2. UNIX for Dummies Questions & Answers

Search file and print everything except multiple search terms

I'm trying to find a way to search a range of similar words in a file. I tried using sed but can't get it right:sed 's/\(ca01\)*//'It only removes "ca01" but leaves the rest of the word. I still want the rest of the information on the lines just not these specific words listed below. Any... (3 Replies)
Discussion started by: seekryts15
3 Replies

3. UNIX for Dummies Questions & Answers

Grep? - using a file of terms to search another file when the information is on a different line

I have a flat file that looks like this, let's call it Chromosome_9.txt: FT /Gene_Name="Guanyl-Acetylase 9" FT /Gene_Number"36952" FT /Gene_Name="Endoplasmic Luciferase" FT /Gene_Number"36953" FT ... (4 Replies)
Discussion started by: Twinklefingers
4 Replies

4. Shell Programming and Scripting

Whether we can search multiple strings using or in grep -F

Hi, Whether we can search multiple strings using or in grep -F In Generally, grep -F "string1" "filename.txt" How to search for multiple string using grep -F as we using grep grep "string1\|string2" "filename.txt" Regards, Nanthagopal A (10 Replies)
Discussion started by: nanthagopal
10 Replies

5. Shell Programming and Scripting

Grep multiple terms and output to individual files

Hi all, I'll like to search a list of tems in a huge file and then output each of the terms to individual files. I know I can use grep -f list main.file to search them but how can I split the output into individual files? Thank you. (6 Replies)
Discussion started by: ivpz
6 Replies

6. Shell Programming and Scripting

Grepping multiple terms with different arguments

Grep -e 'term1' -A1 -e 'term2' -A3The above code always searches for either term and prints results + next three lines. I'm trying to print out: foo foo foo term1 bar bar bar line right after the above -- la la la la term2 so so so line right after the above and again and again I've... (7 Replies)
Discussion started by: dkozel
7 Replies

7. Shell Programming and Scripting

multiple search keyword in grep

Dear All, I have a file containing info like TID:0903 asdfasldjflsdjf TID:0945 hjhjhkhkhkh TID:2045 hjhjhkhkhkh TID:1945 hjhjhkhkhkh TID:2045 hjhjhkhkhkh I need to show only lines containing TID:0903 asdfasldjflsdjf TID:0945 hjhjhkhkhkh TID:2045 hjhjhkhkhkh TID:2045 hjhjhkhkhkh ... (11 Replies)
Discussion started by: saifurshaon
11 Replies

8. UNIX for Dummies Questions & Answers

Multiple Line search using grep

Hi All, I am trying to search multiple lines in file using grep /sed.And i cant seem to make it work. The File looks like this 5012001,100,AUTOBATCH,FEE,DAILYFEE,0,0 4241 SERVICE DENIED 5012002,100,AUTOBATCH,FEE,DAILYFEE,0,0 4241 SERVICE DENIED... (6 Replies)
Discussion started by: pistachio
6 Replies

9. UNIX for Dummies Questions & Answers

search multiple words using grep

Hi frnds i want to desplay file names that should be word1 and word2 ex : i have 10 *.log files 5 files having word1 and word2 5 files having only word1, i have used below command egrep -l 'word1|word2' *.log its giving all 10 files, but i want to display only 5... (20 Replies)
Discussion started by: pb18798
20 Replies

10. Shell Programming and Scripting

multiple search with grep

is it possible to execute multiple search with grep??? grep criteria1 criteria2 file something like this... I know other way: grep pattern1 file-name1 > results-file grep pattern2 file-name1 >> results-file cat results-file but can it be done with one grep???? (5 Replies)
Discussion started by: amon
5 Replies
Login or Register to Ask a Question