Advance conditional grep command


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Advance conditional grep command
# 1  
Old 09-11-2011
Advance conditional grep command

Hi guys, I need your help to use advance grep command, In my example below, I have 5 container which has some information on it, and I need to grep specific word "PLMNCode=454F00" which will not only grep the line contains that word, but I need to print the output for the whole container.

Input file:

StartOfRecord
dateAndTimeStart = 20110904091315
PLMNCode=455F00
dateAndTimeEnd = 20110904091415
EndOfRecord

StartOfRecord
dateAndTimeStart = 20110904091315
[COLOR="rgb(255, 0, 255)"]PLMNCode=454F00[/COLOR]
dateAndTimeEnd = 20110904091415
EndOfRecord

StartOfRecord
dateAndTimeStart = 20110905142111
[COLOR="rgb(255, 0, 255)"]PLMNCode=455F00[/COLOR]
dateAndTimeEnd = 20110905152111
EndOfRecord

StartOfRecord
dateAndTimeStart = 20110911122110
[COLOR="rgb(255, 0, 255)"]PLMNCode=454F00[/COLOR]
dateAndTimeEnd = 20110911122203
EndOfRecord

StartOfRecord
dateAndTimeStart = 20110911122110
[COLOR="rgb(255, 0, 255)"]PLMNCode=460F00[/COLOR]
dateAndTimeEnd = 20110911122203
EndOfRecord


Example when I grep "PLMNCode=455F00" it should print out like (below), there are 2 container that contains the "PLMNCode=455F00".

StartOfRecord
dateAndTimeStart = 20110904091315
PLMNCode=455F00
dateAndTimeEnd = 20110904091415
EndOfRecord

StartOfRecord
dateAndTimeStart = 20110905142111
[COLOR="rgb(255, 0, 255)"]PLMNCode=455F00[/COLOR]
dateAndTimeEnd = 20110905152111
EndOfRecord


Then finally, it will write on a outfile txt file.

Thank you in advance for your help guys!! This will really help me alot!
# 2  
Old 09-11-2011
If you have GNU grep, then try this
Code:
$ grep -A2 -B2 "PLMNCode=455F00" contents 
StartOfRecord
dateAndTimeStart = 20110904091315
PLMNCode=455F00
dateAndTimeEnd = 20110904091415
EndOfRecord
--
StartOfRecord
dateAndTimeStart = 20110905142111
[COLOR="rgb(255, 0, 255)"]PLMNCode=455F00[/COLOR]
dateAndTimeEnd = 20110905152111
EndOfRecord

---------- Post updated at 08:55 AM ---------- Previous update was at 08:54 AM ----------

Code:
$ grep --version
GNU grep 2.5.4

# 3  
Old 09-11-2011
Thanks for your response! I forgot to mention that there is no line space (as below), will it also work with GNU grep? Thanks again.

StartOfRecord
dateAndTimeStart = 20110904091315
PLMNCode=455F00
dateAndTimeEnd = 20110904091415
EndOfRecord
StartOfRecord
dateAndTimeStart = 20110904091315
[COLOR="rgb(255, 0, 255)"]PLMNCode=454F00[/COLOR]
dateAndTimeEnd = 20110904091415
EndOfRecord
StartOfRecord
dateAndTimeStart = 20110905142111
[COLOR="rgb(255, 0, 255)"]PLMNCode=455F00[/COLOR]
dateAndTimeEnd = 20110905152111
EndOfRecord
StartOfRecord
dateAndTimeStart = 20110911122110
[COLOR="rgb(255, 0, 255)"]PLMNCode=454F00[/COLOR]
dateAndTimeEnd = 20110911122203
EndOfRecord
StartOfRecord
dateAndTimeStart = 20110911122110
[COLOR="rgb(255, 0, 255)"]PLMNCode=460F00[/COLOR]
dateAndTimeEnd = 20110911122203
EndOfRecord
# 4  
Old 09-11-2011
yes, it will
# 5  
Old 09-11-2011
Hi Itkamaraj,

This works like a charm! Just one more question, because if the number of line is not fixed, the example I provided above contains 5 lines per container, what if it occured more than five lines? Is it possible to treat the "StartOfRecord" and starting point then "EndOfRecord" end point?
# 6  
Old 09-11-2011
In that case, it works for you.
Code:
$ awk 'BEGIN{RS="EndOfRecord"} $0~/PLMNCode=455F00/ {print $0"EndOfRecord"}' contents.bk 
StartOfRecord
dateAndTimeStart = 20110904091315
PLMNCode=455F00
dateAndTimeEnd = 20110904091415
EndOfRecord

StartOfRecord
dateAndTimeStart = 20110905142111
[COLOR="rgb(255, 0, 255)"]PLMNCode=455F00[/COLOR]
dateAndTimeEnd = 20110905152111
EndOfRecord

# 7  
Old 09-11-2011
Hi itkamaraj,

Works great! thanks alot for your help, this really saved hours of my work load. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Bashrc File - Conditional Command Execution?

Hello All, I was wondering if there is a way to execute a command in my ".bashrc" file based on how I logged into the PC? I was thinking maybe there is a way to check how the user (*myself) logged in, maybe somehow with the who command along with something else, but I'm not sure... I know I... (7 Replies)
Discussion started by: mrm5102
7 Replies

2. Programming

Python Conditional Statements Based on Success of Last Command

In shell scripting, I can create a conditional statement based on the success or failure (exit status)of a command such as: pinger() { ping -c 2 $remote_host >/dev/null 2>&1 ping_stat=$? } pinger if ]; then echo "blahblahblah" exit 0 fi how is this done using Python using... (3 Replies)
Discussion started by: metallica1973
3 Replies

3. Shell Programming and Scripting

Conditional grep command to search entire file

Let me give you a complete example what I am trying to achieve. 1. Below is the log file structure where I need 2,5 and 14th column of the logs after grepping through the linkId=1ddoic. Log file structure:- abc.com 20120829001415 127.0.0.1 app none11111 sas 0 0 N clk Mozilla/5.0... (3 Replies)
Discussion started by: kmajumder
3 Replies

4. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: redbluefish
3 Replies

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

6. Programming

Advance FIND Command

Hi, I have to find jar file name and its path by passing it containing class file name, jar is a kind of zip. by using following command I am able to get jar file name by passing class file name. find -name "*.jar" -exec grep "CLASSNAME" {} \; find -name "*.jar" -exec grep... (3 Replies)
Discussion started by: niceboykunal123
3 Replies

7. Shell Programming and Scripting

Conditional checking for VMWARE command

Hello, I have to runt he following VMWARE script to take a snap shot of my machine: vmware-cmd -v /vmfs/volumes/44e9c20f-71ded630-3ac2-00137221e12a/orion/orion.vmx createsnapshot Weekly_Backup I need to check if successfully executes or not so I thought I would put it in a IF STATEMENT ... (2 Replies)
Discussion started by: mojoman
2 Replies

8. Shell Programming and Scripting

Conditional mailing as command result

Hi ! I would like to be informed by mail when a ping conmand exceeds a certain avarage value. This is the output of the ping command rtt min/avg/max/mdev = 164.505/165.522/166.540/1.095 ms I need to get the second value (165.522) and if greater than 200, send a mail. This should... (2 Replies)
Discussion started by: gadile
2 Replies

9. UNIX for Advanced & Expert Users

Regarding Advance installation

Hi to all, I have one doubt in unix/linux installation. There are two systems in which one is Sparc system having no CD-Rom, No O/S and fresh Hard disk and another system is having windows O/S. Now, can i install unix or linux o/s in the Empty Sparc System by using other system which is... (1 Reply)
Discussion started by: sasidarvarma
1 Replies

10. Shell Programming and Scripting

Looping and conditional branching on the command line

I am piping STDOUT from commands such as ifconfig and dmesg through grep, sed and awk to get the information I need. I need to now perform some looping and branching now and have been trying to figure out how to do this on the command line. You may ask "Why the command line? - Why not put it... (2 Replies)
Discussion started by: karlgo
2 Replies
Login or Register to Ask a Question