Print text between delimiters IF it contains a certain term...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print text between delimiters IF it contains a certain term...
# 1  
Old 09-07-2011
Print text between delimiters IF it contains a certain term...

So I'm racking my brain on appropriate ways to solve a problem that once fixed, will solve every problem in my life. Its very easy (for you guys and gals) I'm sure, but I can't seem to wrap my mind around the right approach. I really want to use bash to do this, but I can't grasp how I'm going to do it. I have a pretty good handle on how to do it with a long regex in perl, but I'd rather not call that within bash, and I really don't want to add another perl script into the mix. As a sample of what I'm needing, here is some data;

Code:
-------------------------------
15:00:01.194213 IP 6.14.8.18.80 > 21.1.12.11.3311: tcp 183
-------------------------------
15:00:01.201435 IP 21.2.17.16.4918 > 6.15.2.18.80: tcp 0
-------------------------------
15:00:01.235586 IP 16.23.16.12.80 > 21.1.18.11.1519: tcp 141
Content-Type: application/ocsp-response
Content-Length: 3686
Connection: Keep-Alive
-------------------------------
15:00:01.235839 IP 16.25.36.42.80 > 21.1.18.11.119: tcp 1380
-------------------------------
15:00:01.235840 IP 6.235.16.12.80 > 21.1.18.121.1519: tcp 80
-------------------------------

The general idea is that I want to "search" a bit of text like this for certain terms, such as "Content-Length:" and receive the following as the result;

Code:
-------------------------------
15:00:01.235586 IP 16.23.16.12.80 > 21.1.18.11.1519: tcp 141
Content-Type: application/ocsp-response
Content-Length: 3686
Connection: Keep-Alive

So, what you have as the result is a match for the search text in question, as well all other information that falls within that packet's delimiters. Now obviously for small cases such as this, I could easily just do a grep -B 4 or something, but not only does that not work consistently, but it also will not get the end of that "section" before the next delimiter. I will graciously accept any solution but I prefer that it be in bash for my own curiousity and ease of implementation.
# 2  
Old 09-07-2011
one way:
Code:
nawk '{gsub("^--*$", "")}1' myFile  | nawk '/Content-Type/' RS=""

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 09-07-2011
You're the man! It works on this scenario great. I'm going to test it on a large scale, but it definitely looks to fit my needs. I don't have access to nawk, but awk works as well.
# 4  
Old 09-07-2011
Alternatively..
Code:
awk '/Content-Length/{print}' RS='----*' inputfile

# 5  
Old 09-07-2011
Quote:
Originally Posted by michaelrozar17
Alternatively..
Code:
awk '/Content-Length/{print}' RS='----*' inputfile

not all awk-s support RS as a regex - most take RS as a single char only.
# 6  
Old 09-07-2011
Quote:
Originally Posted by michaelrozar17
Alternatively..
Code:
RS='----*'

It should be noted that multi-character RS is supported by limited awk implementations (GNU awk and may be TAWK).
# 7  
Old 09-07-2011
Thanks radoulov and vgersh99 for the information. If all the awk's does not support regex in RS we could try as below..?
Code:
awk '/Content-Length/{sub(/--*/,"");print}' RS='---' inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

find & Replace text using two non-unique delimiters.

I can find and replace text when the delimiters are unique. What I cannot do is replace text using two NON-unique delimiters: Ex., "This html code <text blah >contains <garbage blah blah >. All tags must go,<text > but some must be replaced with <garbage blah blah > without erasing other... (5 Replies)
Discussion started by: bedtime
5 Replies

2. Shell Programming and Scripting

How to put delimiters in text files after fix characters?

Hi , i have a text file in which i want to put delimiters after certain characters ( fix),. like put a delimiter (any like ,) after 1-3 character than 4 than 5 than 6-17 ..... files looks like this (original)... (8 Replies)
Discussion started by: anamdev
8 Replies

3. Shell Programming and Scripting

awk: Print fields between two delimiters on separate lines and send to variables

I have email headers that look like the following. In the end I would like to accomplish sending each email address to its own variable, such as: user1@domain.com='user1@domain.com' user2@domain.com='user2@domain.com' user3@domain.com='user3@domain.com' etc... I know the sed to get rid of... (11 Replies)
Discussion started by: tay9000
11 Replies

4. Shell Programming and Scripting

Print text it contains a certain term...

Hello, I need to extract execution error on a log file. I am on Linux, but I can only use bash or ksh. As a sample of what I'm needing, here is some data; ***** L1 FILE VAX_ASP1_SATx_E_IM20101017035246 - CPU TIME 2011-04-29T09:57:57 ***** GMT_PRODUCT_CREATION_TIME = 2011-04-29T09:57:57... (6 Replies)
Discussion started by: Aswex
6 Replies

5. Shell Programming and Scripting

Order text by delimiters

I try order the content from file by delimiters. This is the text: interface Loopback0 description !!!RID RR_SLT ip address 172.31.128.19 255.255.255.255 interface GigabitEthernet0 description !!!P_SLT GI0/0/9 ip address 172.31.130.246 255.255.255.252 and the result that I need... (11 Replies)
Discussion started by: bobbasystem
11 Replies

6. AIX

Print text between two delimiters

Hi, Can somebody help me with the below situation, Input File, ======== 2007_08_07_IA-0100-014_(MONTHLY).PDF 2007_08_07_IA-0100-031_(QUARTERLY)(RERUN).PDF 2008-02-28_KR-1022-003_(MONTH)(RERUN)(REC1).CSV Required output, ============ MONTHLY QUARTERLY MONTH ... (15 Replies)
Discussion started by: sravicha
15 Replies

7. Programming

c program to extract text between two delimiters from some text file

needa c program to extract text between two delimiters from some text file. and then storing them in to diffrent variables ? text file like 0: abc.txt ========= aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass aaaaaa|11111111|sssssssssss|333333|ddddddddd|34343454564|asass... (7 Replies)
Discussion started by: kukretiabhi13
7 Replies

8. UNIX for Advanced & Expert Users

extract text b/w two delimiters

I have an input file which looks like " @$SCRIPT/atp_asrmt_adj.sql $SCRIPT/dba2000.scr -s / @$SCRIPT/cim1005w.pls $SCRIPT/dba2000.scr -s / @$SCRIPT/cim1006w.pls start $SCRIPT/cim1020d.sql;^M spool $DATA/cim1021m.sql @$DATA/cim1021m.sql ! rm $DATA/cim1021m.sql spool $DATA/cim1021m.sql... (6 Replies)
Discussion started by: dowsed4u8
6 Replies

9. Shell Programming and Scripting

Search term and output term in desired field

Hi All, I have an input_file below and i would like to use Perl to search for the term "aaa" and output the 3rd term in the same row as "aaa".For Example, i want to search for the term "ddd" and would want the code to ouput the 3rd term in the same row which is "fff". Can somebody help ? ... (28 Replies)
Discussion started by: Raynon
28 Replies

10. Programming

Create a Term & Run chars on this Term

hi floks ! i'd like to know how can i transmete a character or a string from my source code to a term and make it interpret or un by the shell wich is running in my term. I'd like to create a Term from my code (and get its file descriptor) and then transmete each char typed on the keyboard to... (1 Reply)
Discussion started by: the_tical
1 Replies
Login or Register to Ask a Question