Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


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

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-05-2012
Registered User
 
Join Date: Jul 2008
Posts: 68
Thanks: 0
Thanked 0 Times in 0 Posts
Display blocks containing specific pattern

Hi,

I have a file containing multiple entries. Each block starts with <BEGIN and ends with <END. Sample data is given below


Code:
<BEGIN
    IMSI=095001202630;
    MSISDN=00145132916;
    DEFCALL=TS11;
    CURRENTNAM=BOTH;
    CAT=COMMON;
    TBS=TS11&TS12&TS21&TS22;
    CARDTYPE=SIM;
    VLRLIST=200;
    CLIPOC=NO;
    LCS=1;
   OCSI=131;
  TCSI=88;   
    UCSI=28;
    SUBRES=ALLPLMN;
    SUB_AGE=0;
    UPL_TIME=1340536017;
    GPRSUPL_TIME=1339578803;
    CHARGE_GLOBAL=NONE;
<END
<BEGIN
    IMSI=095001202630;
    MSISDN=00145132916;
    DEFCALL=TS11;
    CURRENTNAM=BOTH;
    CAT=COMMON;
    TBS=TS11&TS12&TS21&TS22;
    CARDTYPE=SIM;
    VLRLIST=200;
    CLIPOC=NO;
    LCS=1;
    OCSI=131;
    SUBRES=ALLPLMN;
    SUB_AGE=0;
    UPL_TIME=1340536017;
    GPRSUPL_TIME=1339578803;
    CHARGE_GLOBAL=NONE;
<END

I want to display all lines b/w <BEGIN and <END if OCSI=131 and TCSI= field is not present. In above data, result should be second block as OCSI is 131 and TCSI field is not present. I can get the all lines b/w BEGIN and END using sed but at loss on how to proceed further. Thanks

Last edited by Scrutinizer; 07-05-2012 at 07:44 PM.. Reason: quote tags -> code tags
Sponsored Links
    #2  
Old 07-06-2012
elixir_sinari's Avatar
Gotham Knight
 
Join Date: Mar 2012
Location: India
Posts: 1,370
Thanks: 87
Thanked 475 Times in 455 Posts

Code:
sed -n '/<BEGIN/,/<END/{
H
/<END/{
 s/.*//
 x
 /\n *TCSI=/! {
 /\n *OCSI=131.\n/ {
  s/^\n//
  p
 }
}
}
}' inputfile

Sponsored Links
    #3  
Old 07-06-2012
Peasant's Avatar
Registered User
 
Join Date: Mar 2011
Posts: 507
Thanks: 14
Thanked 104 Times in 102 Posts
You can try this in gawk

Code:
awk 'BEGIN {RS="<BEGIN"}  $0 !~ /TCSI=/ && /OCSI=131/ { print RS $0 } '

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
How to Detect Specific Pattern and Print the Specific String after It? radynaraya UNIX for Dummies Questions & Answers 3 06-13-2012 09:31 AM
how to split this file into blocks and then send these blocks as input to the tool called Yices? paramad Shell Programming and Scripting 5 12-17-2010 01:35 AM
Convert 512-blocks to 4k blocks rockycj UNIX for Dummies Questions & Answers 2 08-03-2010 06:24 PM
Want to grep for a pattern and display the content above that pattern ajayakunuri Shell Programming and Scripting 6 06-25-2010 06:48 AM
how to display specific lines of a specific file raidkridley UNIX for Dummies Questions & Answers 2 10-15-2008 02:46 PM



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