Display lines between keywords inclusive


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Display lines between keywords inclusive
# 1  
Old 12-21-2013
Display lines between keywords inclusive

Hi,

I have a file that contains a lot of lines that are repetitive. I am interested in grepping the following section which is repetitive.



Code:
:::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::
<Address>
   <Street>xxxy</Street>
   ::::::::::::::::::::::::::::::
   ::::::::::::::::::::::::::::::
   <MainMAP>xxx</MainMAP>
   <PrimMAP>yyy</PrimeMAP>
   <SecdMAP>zzz</SecdMAP>
   ::::::::::::::::::::::::::::::
   ::::::::::::::::::::::::::::::
   <Street>xxxz</Street>
   <MainMAP>AAA</MainMAP>
   <PrimMAP>BBB</PrimeMAP>
   <SecdMAP>CCC</SecdMAP>
   ::::::::::::::::::::::::::::::
   ::::::::::::::::::::::::::::::
</Address>
:::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::

My final output should just grep Street tag and MAP tag between Address tag and file should look like
Code:
<Street>xxxy</Street>
<MainMAP>xxx</MainMAP>
<PrimMAP>yyy</PrimeMAP>
<SecdMAP>zzz</SecdMAP>
newline
<Street>xxxz</Street>
<MainMAP>AAA</MainMAP>
<PrimMAP>BBB</PrimeMAP>
<SecdMAP>CCC</SecdMAP>


Thanks,
JaK
# 2  
Old 12-22-2013
What have you tried so far?

And, some questions about your data:
  1. Are the Street, MainMAP, PrimMAP, and SecdMap start and end tags always on the same line in your input files as they are in your sample?
  2. Are there ever any other '<' or '>' characters between the start and end tags for the Street, MainMAP, PrimMAP, and SecdMAP tags? (There aren't any in your sample input file.)
  3. Are each of these tags always on lines by themselves as they are in your sample input file (i.e., no other tags before or after them and only zero or more spaces and tabs before the opening tag and after the closing tag)?
# 3  
Old 12-22-2013
Hi,

So far i am doing manual steps

Step 1: Replace Street with Street:
Step 2: Replace MAP with MAP:
Step 3: grep ":" myfile.xml
Step 4: Replace : with nothing (%s/://g)


Thanks,
JaK

Last edited by jakSun8; 12-22-2013 at 11:06 AM..
# 4  
Old 12-22-2013
For your simple input example - which is questioned by Don Cragun - you may want to try this (which is based on some assumptions):
Code:
sed -rn  '/<Address>/,/<\/Address>/{/SecdMAP/s/$/\n/;/Street|MAP/p}'  file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

DH key size must be multiple of 64, and can only range from 512 to 2048 (inclusive).

Hello, I have a set-up in which I am executing a build from Jenkins on a Solaris 10 Server connecting via sshexec task in ANT. On triggering the build, it is throwing below error: com.jcraft.jsch.JSchException: Session.connect: java.security.InvalidAlgorithmParameterException: DH key size... (2 Replies)
Discussion started by: nishant.kansal@
2 Replies

2. UNIX for Dummies Questions & Answers

Copy Lines between Keywords & paste them to another file

hi, I have Multiple files with the following data : File1 100414 DR1 END XXXXX Test1 Test2 Test3 Test4 Test5 Test6 END 100514 DR2 END XXXXX Test7 Test8 Test9 Test10 Test11 Test12 END 100614 DR3 (5 Replies)
Discussion started by: newageBATMAN
5 Replies

3. Shell Programming and Scripting

Extracting words and lines based on keywords

Hello! I'm trying to process a text file and am stuck at 2 extractions. Hoping someone can help me here: 1. Given a line in a text file and given a keyword, how can I extract the word preceeding the keyword using a shell command/script? For example: Given a keyword "world" in the line: ... (2 Replies)
Discussion started by: seemad
2 Replies

4. Shell Programming and Scripting

Extract lines from file using keywords using script

Hi I need some lines of text from input file using keywords. Inputfile IP IS 10.238.52.65 pun-ras-bng-mhs-01#context bsnl.in Card Status : 1:0, 2:1, 3:1, 4:1, 5:0, 6:0, 7:0, 8:0, 9:1, 10:0, 11:0, 12:0, 13:0, 14:1, Max Circuits: 1: 0, 2: 32768, ... (5 Replies)
Discussion started by: surender reddy
5 Replies

5. Shell Programming and Scripting

List of files in a folder inclusive subfolder

Hi, I need to list the names of existing files in a specific folder. I have written a script for that, but the problem is, it is also picking up name of a subfolder that is there in that folder. I need only the list of files and not that subfolder. How to go about that ? Can anyone plz help... (2 Replies)
Discussion started by: Subhasis
2 Replies

6. UNIX for Dummies Questions & Answers

Reading specific lines from a file using index or keywords

Hello I want to read from a file which contains email addresses. The file format is like this. from@mail.com to1@mail.com to2@mail.com cc@mail.com bcc@mail.com I'll have to read from such file and assign the email addresses to respective variables. frommail =... (11 Replies)
Discussion started by: Kyaw Lwin Phyo
11 Replies

7. Shell Programming and Scripting

copy range of lines in a file based on keywords from another file

Hi Guys, I have the following problem. I have original file (org.txt) that looks like this module v_1(.....) //arbitrary number of text lines endmodule module v_2(....) //arbitrary number of text lines endmodule module v_3(...) //arbitrary number of text lines endmodule module... (6 Replies)
Discussion started by: kaaliakahn
6 Replies

8. Shell Programming and Scripting

Sorting lines based on keywords for MySQL script

the thing which i require is very very complex.. i tried hard to find the solution but couldnt.. the thing i need to achieve is say i have a file cat delta.sql CREATE VIEW Austin Etc etc . . . CREATE VIEW Barabara AS SELECT blah blah blah FROM Austin z, Cluster s, Instance i WHERE... (4 Replies)
Discussion started by: vivek d r
4 Replies

9. Shell Programming and Scripting

How to search for keywords in subsequent lines

Hi all, I am looking for a coomand to search for the keywords in susequenct lines. Keyword1 in a line and Keyword2 in the very next line. Once i found the combination ineed to print the lines with patterns and the line above and one below. I am giving an example here: Keywords are :ERROR and... (12 Replies)
Discussion started by: rdhanek
12 Replies

10. Shell Programming and Scripting

Capture lines based on keywords

Hello everyone, I am trying to write a script that will capture few lines from a text file based on 2 keywords in the first line and 1 keyword in the last one. It could also be based on the first line only + the folllowing 3 lines. Could some one help or give directions. Thanks. (4 Replies)
Discussion started by: nimo
4 Replies
Login or Register to Ask a Question