Multiple Line search using grep | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Multiple Line search using grep

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-05-2008
pistachio pistachio is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 5 August 2014, 6:35 AM EDT
Posts: 28
Thanks: 2
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old 09-05-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 10 Times in 8 Posts
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.
Sponsored Links
    #3  
Old 09-05-2008
pistachio pistachio is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 5 August 2014, 6:35 AM EDT
Posts: 28
Thanks: 2
Thanked 0 Times in 0 Posts
MySQL Thanks

Thanks this works.Though i want to understand how the command works.
    #4  
Old 09-05-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
 
Join Date: Mar 2008
Last Activity: 28 March 2011, 6:41 AM EDT
Location: /there/is/only/bin/sh
Posts: 3,653
Thanks: 0
Thanked 10 Times in 8 Posts
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.
Sponsored Links
    #5  
Old 09-05-2008
pistachio pistachio is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 5 August 2014, 6:35 AM EDT
Posts: 28
Thanks: 2
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #6  
Old 09-06-2008
drl's Avatar
drl drl is offline Forum Advisor  
Registered Voter
 
Join Date: Apr 2007
Last Activity: 1 September 2014, 3:46 PM EDT
Location: Saint Paul, MN USA / BSD, CentOS, Debian, OS X, Solaris
Posts: 1,670
Thanks: 39
Thanked 192 Times in 175 Posts
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
Sponsored Links
    #7  
Old 09-06-2008
ghostdog74 ghostdog74 is offline
Registered User
 
Join Date: Sep 2006
Last Activity: 7 November 2013, 6:42 AM EST
Posts: 2,651
Thanks: 0
Thanked 17 Times in 17 Posts

Code:
while read -r line
do
    case $line in
        50* ) l=$line;;
        4241* ) echo $l; echo $line;;
    esac
done < file

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep multiple search terms with context Moe.Wilensky Shell Programming and Scripting 1 04-20-2012 04:52 PM
Help to search multiple pattern in file with grep/sed/awk gr8_usk Shell Programming and Scripting 9 07-21-2011 11:31 AM
multiple search keyword in grep saifurshaon Shell Programming and Scripting 11 03-19-2009 05:56 AM
search multiple words using grep pb18798 UNIX for Dummies Questions & Answers 20 08-26-2008 01:43 AM
multiple search with grep amon Shell Programming and Scripting 5 04-04-2006 06:53 AM



All times are GMT -4. The time now is 02:45 AM.