Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Multiple Line search using grep Post 302233153 by drl on Saturday 6th of September 2008 10:34:21 AM
Old 09-06-2008
Hi.

Here are a few solutions to the original problem using cgrep:
Code:
#!/bin/bash -

# @(#) s1       Demonstrate cgrep for complex string matching.

# See:
# http://www.bell-labs.com/project/wwexptools/cgrep/

echo
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) cgrep
set -o nounset
echo

FILE=${1-data1}

echo " Data file $FILE:"
cat $FILE

echo
echo " Results, match 42, print window pattern backward:"
cgrep -w '^5012' '^4241' $FILE

echo
echo " Results, match 42, print window line backward:"
cgrep -1 '^4241' $FILE

echo
echo " Results, match 50 followed by 4241 across lines:"
cgrep -a "^5012.*\n*.*4241" $FILE

exit 0

Producing:
Code:
% ./s1

(Versions displayed with local utility "version")
Linux 2.6.11-x1
GNU bash 2.05b.0
cgrep (local) - no version provided for ~/executable/cgrep.

 Data file data1:
5012001,100,AUTOBATCH,FEE,DAILYFEE,0,0 1
4241 SERVICE DENIED
5012002,100,AUTOBATCH,FEE,DAILYFEE,0,0 2
4241 SERVICE DENIED
5012003,100,AUTOBATCH,FEE,DAILYFEE,0,0 3
4241 SERVICE DENIED
5012001,100,AUTOBATCH,FEE,DAILYFEE,0,0 4
2001 DIAMETER SUCCESS
5012002,100,AUTOBATCH,FEE,DAILYFEE,0,0 5
2001 DIAMETER SUCCESS
5012003,100,AUTOBATCH,FEE,DAILYFEE,0,0 6
2001 DIAMETER SUCCESS
5012001,100,AUTOBATCH,FEE,DAILYFEE,0,0 7
2001 DIAMETER SUCCESS
5012002,100,AUTOBATCH,FEE,DAILYFEE,0,0 8
2001 DIAMETER SUCCESS

 Results, match 42, print window pattern backward:
5012001,100,AUTOBATCH,FEE,DAILYFEE,0,0 1
4241 SERVICE DENIED
5012002,100,AUTOBATCH,FEE,DAILYFEE,0,0 2
4241 SERVICE DENIED
5012003,100,AUTOBATCH,FEE,DAILYFEE,0,0 3
4241 SERVICE DENIED

 Results, match 42, print window line backward:
5012001,100,AUTOBATCH,FEE,DAILYFEE,0,0 1
4241 SERVICE DENIED
5012002,100,AUTOBATCH,FEE,DAILYFEE,0,0 2
4241 SERVICE DENIED
5012003,100,AUTOBATCH,FEE,DAILYFEE,0,0 3
4241 SERVICE DENIED

 Results, match 50 followed by 4241 across lines:
5012001,100,AUTOBATCH,FEE,DAILYFEE,0,0 1
4241 SERVICE DENIED
5012002,100,AUTOBATCH,FEE,DAILYFEE,0,0 2
4241 SERVICE DENIED
5012003,100,AUTOBATCH,FEE,DAILYFEE,0,0 3
4241 SERVICE DENIED

I augmented the original data file so that one could see which of the duplicate lines were extracted ... cheers, drl
 

10 More Discussions You Might Find Interesting

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

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

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

4. Shell Programming and Scripting

Grep multiple search terms with context

I have a file that is a sort library in the format: ##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: (1 Reply)
Discussion started by: Moe.Wilensky
1 Replies

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

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

7. Shell Programming and Scripting

Multiple line search, replace second line, using awk or sed

All, I appreciate any help you can offer here as this is well beyond my grasp of awk/sed... I have an input file similar to: &LOG &LOG Part: "@DB/TC10000021855/--F" &LOG &LOG &LOG Part: "@DB/TC10000021852/--F" &LOG Cloning_Action: RETAIN &LOG Part: "@DB/TCCP000010713/--A" &LOG &LOG... (5 Replies)
Discussion started by: KarmaPoliceT2
5 Replies

8. Linux

How to search multiple word using grep command?

How to search multiple word using grep command for example i want to reserch ANJ001 AA Using ridiculous font, size, and color changes instead of normal space separated text and CODE tags obfuscates what you are trying to do and makes it difficult for volunteers who may want to help you solve... (1 Reply)
Discussion started by: na.dharma
1 Replies

9. Shell Programming and Scripting

Search words in multiple file line by line

Hi All I have to search servers name say like 1000+ "unique names" line by line in child.txt files in another file that is a master file where all server present say "master.txt",if child.txt's server name matches with master files then it print yes else no with server name. (4 Replies)
Discussion started by: netdbaind
4 Replies

10. 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
GREP(1) 						      General Commands Manual							   GREP(1)

NAME
grep - search a file for lines containing a given pattern SYNOPSIS
grep [-elnsv] pattern [file] ... OPTIONS
-e -e pattern is the same as pattern -c Print a count of lines matched -i Ignore case -l Print file names, no lines -n Print line numbers -s Status only, no printed output -v Select lines that do not match EXAMPLES
grep mouse file # Find lines in file containing mouse grep [0-9] file # Print lines containing a digit DESCRIPTION
Grep searches one or more files (by default, stdin) and selects out all the lines that match the pattern. All the regular expressions accepted by ed and mined are allowed. In addition, + can be used instead of * to mean 1 or more occurrences, ? can be used to mean 0 or 1 occurrences, and | can be used between two regular expressions to mean either one of them. Parentheses can be used for grouping. If a match is found, exit status 0 is returned. If no match is found, exit status 1 is returned. If an error is detected, exit status 2 is returned. SEE ALSO
cgrep(1), fgrep(1), sed(1), awk(9). GREP(1)
All times are GMT -4. The time now is 09:11 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy