Getting lines between two strings with duplicate set of data


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting lines between two strings with duplicate set of data
# 1  
Old 10-01-2013
Getting lines between two strings with duplicate set of data

if I have the following lines in a file app.log

some lines here
Code:
<AAAA>
abc
<id>123456789</id>
ddd
</AAAA>

some lines here too
Code:
<BBBB>
abc
<id>123456789</id>
ddd
</BBBB>

some lines here too
Code:
<AAAA>
xyz
<id>987654321</id>
ssss
</AAAA>

some lines here again...

How do I get the particular response that I am interested in by providing the id ?
like if I am interested in AAAA response for id 123456789 like below
Code:
<AAAA>
abc
<id>123456789</id>
ddd
</AAAA>

I might need some changes to the command that I am using. it gives me all AAAA responses, but I need to filter the response having the id 123456789

Code:
cat app.log | sed -n '/<AAAA/,/<\/AAAA>/p'

Thanks in advance.

Narayana.V

Last edited by nariwithu; 10-01-2013 at 12:08 PM..
# 2  
Old 10-01-2013
This may work, at least on your simplified samples:
Code:
awk '/<AAAA>/,/<\/AAAA>/ {X=X"\n"$0} /<\/AAAA>/ && X ~ /123456789/ {print X; X=""}' file

<AAAA>
abc
<id>123456789</id>
ddd
</AAAA>

This User Gave Thanks to RudiC For This Post:
# 3  
Old 10-01-2013
Rudic - It is giving me all AAAA responses
# 4  
Old 10-01-2013
Then your sample file may not be representative.
# 5  
Old 10-01-2013
X should be cleared at the end of every block
Code:
awk '/<AAAA>/,/<\/AAAA>/{X=X"\n"$0}
/<\/AAAA>/ {if (X~/123456789/) print X; X=""}' file

This User Gave Thanks to MadeInGermany For This Post:
# 6  
Old 10-01-2013
Thanks MadeinGermany, It is working perfect.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove lines containing 2 or more duplicate strings

Within my text file i have several thousand lines of text with some lines containing duplicate strings/words. I would like to entirely remove those lines which contain the duplicate strings. Eg; One and a Two Unix.com is the Best This as a Line Line Example duplicate sentence with the word... (22 Replies)
Discussion started by: martinsmith
22 Replies

2. Shell Programming and Scripting

Help needed with shell script to search and replace a set of strings among the set of files

Hi, I am looking for a shell script which serves the below purpose. Please find below the algorithm for the same and any help on this would be highly appreciated. 1)set of strings need to be replaced among set of files(directory may contain different types of files) 2)It should search for... (10 Replies)
Discussion started by: Amulya
10 Replies

3. Shell Programming and Scripting

Delete duplicate strings in a line

Hi, i need help to remove duplicates in my file. The problem is i need to delete one duplicate for each line only. the input file as follows and it is not tab delimited:- The output need to remove 2nd word (in red) that duplicate with 1st word (in blue). Other duplicates should remained... (12 Replies)
Discussion started by: redse171
12 Replies

4. UNIX for Dummies Questions & Answers

Removing a set of Duplicate lines from a file

Hi, How do i remove a set of duplicate lines from a file. My file contains the lines: abc def ghi abc def ghi jkl mno pqr jkl mno (1 Reply)
Discussion started by: raosr020
1 Replies

5. Shell Programming and Scripting

Print specific lines of a repeated set of data

I have a file that needs 1st line, 2nd line, and 26th line printed from every chunk of data. Each chunk of data contains 26 lines (#line+%line+24 data lines = 26 lines of data repeated). Input file: # This is a data file used for blockA (chunk 1). % 10576 A 10 0 1 04 (data1) 03 (data2)... (2 Replies)
Discussion started by: morrbie
2 Replies

6. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

7. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

8. Shell Programming and Scripting

Delete duplicate data and pertain the latest month data.

Hi I have a file with following records It contains three months of data, some data is duplicated,i need to access the latest data from the duplicate ones. for e.g; i have foll data "200","0","","11722","-63","","","","11722","JUL","09" "200","0","","11722","-63","","","","11722","JUL","09"... (10 Replies)
Discussion started by: vee_789
10 Replies

9. UNIX for Dummies Questions & Answers

Delete lines with duplicate strings based on date

Hey all, a relative bash/script newbie trying solve a problem. I've got a text file with lots of lines that I've been able to clean up and format with awk/sed/cut, but now I'd like to remove the lines with duplicate usernames based on time stamp. Here's what the data looks like 2007-11-03... (3 Replies)
Discussion started by: mattv
3 Replies
Login or Register to Ask a Question