Display blocks containing specific pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Display blocks containing specific pattern
# 1  
Old 07-05-2012
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 08:44 PM.. Reason: quote tags -> code tags
# 2  
Old 07-06-2012
Code:
sed -n '/<BEGIN/,/<END/{
H
/<END/{
 s/.*//
 x
 /\n *TCSI=/! {
 /\n *OCSI=131.\n/ {
  s/^\n//
  p
 }
}
}
}' inputfile

# 3  
Old 07-06-2012
You can try this in gawk
Code:
awk 'BEGIN {RS="<BEGIN"}  $0 !~ /TCSI=/ && /OCSI=131/ { print RS $0 } '

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract whole word preceding a specific character pattern with first occurence of the pattern

Hello. Here is a file contents : declare -Ax NEW_FORCE_IGNORE_ARRAY=(="§" ="§" ="§" ="§" ="§" .................. ="§"Here is a pattern =I want to extract 'NEW_FORCE_IGNORE_ARRAY' which is the whole word before the first occurrence of pattern '=' Is there a better solution than mine :... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

Swapping columns in specific blocks

Hi all, I hope all you guys have a great new year! I am trying to swap 2 columns in a specific block of a file. The file format is: Startpoint: in11 (input port) Endpoint: out421 (output port) Path Group: (none) Path Type: max Point ... (5 Replies)
Discussion started by: jypark22
5 Replies

3. Shell Programming and Scripting

sed - filter blocks between single delimiters matching a pattern

Hi! I have a file with the following format:CDR ... MSISDN=111 ... CDR ... MSISDN=xxx ... CDR ... MSISDN=xxx ... CDR ... MSISDN=111 (2 Replies)
Discussion started by: Flavius
2 Replies

4. UNIX for Dummies Questions & Answers

Quick UNIX command to display specific lines in the middle of a file from/to specific word

This could be a really dummy question. I have a log text file. What unix command to extract line from specific string to another specific string. Is it something similar to?: more +/"string" file_name Thanks (4 Replies)
Discussion started by: aku
4 Replies

5. Shell Programming and Scripting

sed multiple multi line blocks of text containing pattern

Hi, I have a log file which has sessionids in it, each block in the log starts with a date entry, a block may be a single line or multiple lines. I need to sed (or awk) out the lines/blocks with that start with a date and include the session id. The files are large at several Gb. My... (3 Replies)
Discussion started by: andyatit
3 Replies

6. UNIX for Dummies Questions & Answers

How to Detect Specific Pattern and Print the Specific String after It?

I'm still beginner and maybe someone can help me. I have this input: the great warrior a, b, c and what i want to know is, with awk, how can i detect the string with 'warrior' string on it and print the a, b, and c seperately, become like this : Warrior Type a b c Im still very... (3 Replies)
Discussion started by: radynaraya
3 Replies

7. Shell Programming and Scripting

how to split this file into blocks and then send these blocks as input to the tool called Yices?

Hello, I have a file like this: FILE.TXT: (define argc :: int) (assert ( > argc 1)) (assert ( = argc 1)) <check> # (define c :: float) (assert ( > c 0)) (assert ( = c 0)) <check> # now, i want to separate each block('#' is the delimeter), make them separate files, and then send them as... (5 Replies)
Discussion started by: paramad
5 Replies

8. UNIX for Dummies Questions & Answers

Convert 512-blocks to 4k blocks

I'm Unix. I'm looking at "df" on Unix now and below is an example. It's lists the filesystems out in 512-blocks, I need this in 4k blocks. Is there a way to do this in Unix or do I manually convert and how? So for container 1 there is 7,340,032 in size in 512-blocks. What would the 4k block be... (2 Replies)
Discussion started by: rockycj
2 Replies

9. Shell Programming and Scripting

Want to grep for a pattern and display the content above that pattern

Hi, When we have a failure, sometimes we just step restart the job from the next step. Later when we open the log for analysis of the failure, it is becoming difficult to go to the failure part. For eg., if it is a 1000 line log, the failure may be at 500th line. so wat i want to do is, grep... (6 Replies)
Discussion started by: ajayakunuri
6 Replies

10. UNIX for Dummies Questions & Answers

how to display specific lines of a specific file

are there any basic commands that can display lines 99 - 101 of the /etc/passwd file? I'm thinking use of head and tail, but I forget what numbers to use and where to put /etc/passwd in the command. (2 Replies)
Discussion started by: raidkridley
2 Replies
Login or Register to Ask a Question