Multiple Line search using grep


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Multiple Line search using grep
# 1  
Old 09-05-2008
Tools 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
5012003,100,AUTOBATCH,FEE,DAILYFEE,0,0
4241 SERVICE DENIED
5012001,100,AUTOBATCH,FEE,DAILYFEE,0,0
2001 DIAMETER SUCCESS
5012002,100,AUTOBATCH,FEE,DAILYFEE,0,0
2001 DIAMETER SUCCESS
5012003,100,AUTOBATCH,FEE,DAILYFEE,0,0
2001 DIAMETER SUCCESS
5012001,100,AUTOBATCH,FEE,DAILYFEE,0,0
2001 DIAMETER SUCCESS
5012002,100,AUTOBATCH,FEE,DAILYFEE,0,0
2001 DIAMETER SUCCESS
5012003,100,AUTOBATCH,FEE,DAILYFEE,0,0


and i want the following output (containing numbers starting with only 50 in the first line have error code 4241 in the next line)


5012001,100,AUTOBATCH,FEE,DAILYFEE,0,0
4241 SERVICE DENIED
5012002,100,AUTOBATCH,FEE,DAILYFEE,0,0
4241 SERVICE DENIED
5012003,100,AUTOBATCH,FEE,DAILYFEE,0,0
4241 SERVICE DENIED

Thanks
Pistachio
# 2  
Old 09-05-2008
Massaging your file into a line-oriented format might make such things easier. It's doable with sed, though.

Code:
sed -n -e '/^50/h' -e '/^4241 /{;x;p;x;p;n;}' file

If there are error codes starting with 50, you might need to tweak the regular expressions slightly.
# 3  
Old 09-05-2008
MySQL Thanks

Thanks this works.Though i want to understand how the command works.
# 4  
Old 09-05-2008
Sed has two "registers", called the "hold space" and the "pattern space". Normally the current input line is in the pattern space. The script copies the current line to the hold space if it sees "50" at beginning of line, and prints the hold space and the pattern space when it sees "4142 " at beginning of line.
# 5  
Old 09-05-2008
Question cool!!

Thanks for the tip.But u just made me more interested in sed.

how will we do this then

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
5012003,100,AUTOBATCH,FEE,DAILYFEE,0,0
4241 SERVICE DENIED
5012001,100,AUTOBATCH,FEE,DAILYFEE,0,0
2001 DIAMETER SUCCESS
5012002,100,AUTOBATCH,FEE,DAILYFEE,0,0
2001 DIAMETER SUCCESS
5012003,100,AUTOBATCH,FEE,DAILYFEE,0,0
2001 DIAMETER SUCCESS
5012001,100,AUTOBATCH,FEE,DAILYFEE,0,0
2001 DIAMETER SUCCESS
5012002,100,AUTOBATCH,FEE,DAILYFEE,0,0
2001 DIAMETER SUCCESS
5012003,100,AUTOBATCH,FEE,DAILYFEE,0,0


and i want the following output (containing numbers starting with only 50 in the first line have error code 4241 in the next line and thrid line containing number starting with 50 and fourth line having code 2001)


5012001,100,AUTOBATCH,FEE,DAILYFEE,0,0
4241 SERVICE DENIED
5012001,100,AUTOBATCH,FEE,DAILYFEE,0,0
2001 DIAMETER SUCCESS
5012002,100,AUTOBATCH,FEE,DAILYFEE,0,0
4241 SERVICE DENIED
5012003,100,AUTOBATCH,FEE,DAILYFEE,0,0
2001 SERVICE DENIED

Thanks
Pistachio
Image
# 6  
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
# 7  
Old 09-06-2008
Code:
while read -r line
do
    case $line in
        50* ) l=$line;;
        4241* ) echo $l; echo $line;;
    esac
done < file

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

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

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

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

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

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

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

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