Match from one pattern to second occurrence of second pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match from one pattern to second occurrence of second pattern
# 1  
Old 01-24-2013
Question Match from one pattern to second occurrence of second pattern

Given an XML file that contains (NOT "consists of"):

Code:
                </dict>
                <key>system.</key>
                <dict>
                        <key>rule</key>
                        <string>default</string>
                </dict>
                <key>system.burn</key>
                <dict>
                        <key>class</key>
                        <string>allow</string>
                        <key>comment</key>
                        <string>For burning media.</string>
                        <key>default-button</key>
                        <dict>
                                <key>ar</key>
                                <string>نسخ قرص</string>
                                <key>ca</key>
                                <string>Gravar</string>
                                <key>cs</key>
                                <string>Vypálit</string>
                                <key>da</key>
                                <string>Brćnd</string>
                                <key>de</key>

I want to isolate and operate on the lines between <key>system.burn</key> and the second occurrence of <dict>, so:

Code:
                        <key>class</key>
                        <string>allow</string>
                        <key>comment</key>
                        <string>For burning media.</string>
                        <key>default-button</key>

Now, sed -n '/<key>system.burn<\/key>/,/<dict>/2 p' /etc/authorization would get me what I want... except I'm on a Mac, and so do not have GNU see. That gets me "invalid command code 2" I can delete the 2 and wind up with a valid pattern, but it only returns:

Code:
		<key>system.burn</key>
		<dict>

I don't want to just return the six lines after <key>system.burn</key> as I cannot be positive that there will always just be six lines.

I have a feeling awk is going to be the answer here, but I've got a long curve to climb with awk. I'm proud of myself for being able to print $2 or even $NF, so that tells you how advanced I am there :-P

Last edited by jnojr; 01-24-2013 at 07:05 PM..
# 2  
Old 01-24-2013
Code:
awk 'BEGIN{c=0}/<dict>/{++c}c==2&&!/dict/{print $0}' xmlfile

# 3  
Old 01-24-2013
Quote:
Originally Posted by bipinajith
Code:
awk 'BEGIN{c=0}/<dict>/{++c}c==2&&!/dict/{print $0}' xmlfile

That returns:

Code:
		<key></key>

I can do:

awk '/<key>system.burn<\/key>/,/<dict>/' /etc/authorization

And end up with the same result result as with sed. I'm trying to figure out how to add the counting logic to that, but it's making my head hurt :-)
# 4  
Old 01-24-2013
Is that the complete XML you posted?

Here is the output when I run awk:
Code:
$ awk 'BEGIN{c=0}/<dict>/{++c}c==2&&!/dict/{print $0}' xml
                        <key>class</key>
                        <string>allow</string>
                        <key>comment</key>
                        <string>For burning media.</string>
                        <key>default-button</key>

# 5  
Old 01-24-2013
Quote:
Originally Posted by bipinajith
Is that the complete XML you posted?
No. It's a snippet from a file with 8700+ lines, with many repetitions of <key>, <dict>, etc.
# 6  
Old 01-24-2013
How about this code?
Code:
awk '/<key>system.burn<\/key>/{f=1;}f==1&&/<dict>/{++c;next}c==1{print}' xmlfile

# 7  
Old 01-24-2013
Quote:
Originally Posted by bipinajith
How about this code?
Code:
awk '/<key>system.burn<\/key>/{f=1;}f==1&&/<dict>/{++c;next}c==1{print}' xmlfile

Awesome!

Now, can I "stack" awk commands, sort of like '-e' in sed? Is that what "END" would be fore, to act on the results of the body?

I tried

Code:
awk '/<key>system.burn<\/key>/{f=1;}f==1&&/<dict>/{++c;next}c==1{print}' '/<key>class<\/key>/{print "                        <key>allow-root<\/key>\n                      <true\/>"' /tmp/authorization

But that read my second pattern as a file and bombed.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk command to get file content until 2 occurrence of pattern match

AWK command to get file content until 3 occurrence of pattern match, INPUT FILE: JMS_BODY_FIELD:JMSText = <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <custOptIn xmlns="http://com/walm/ta/cu/ccs/xml2"> <person>Romi</person> <appName>SAP</appName> </custOptIn> ... (4 Replies)
Discussion started by: prince1987
4 Replies

2. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

3. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

4. Shell Programming and Scripting

sed : match one pattern then the next consecutive second pattern not working

Ive used this snippet of code on a solaris box thousands of times. But it isnt working on the new linux box sed -n '/interface LoopBack0/{N;/ ip address /p;}' *.conf its driving me nuts !! Is there something Im missing ? (7 Replies)
Discussion started by: popeye
7 Replies

5. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

6. Shell Programming and Scripting

Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04

Hi, I am getting crazy after days on looking at it: Bash in Ubuntu 12.04.1 I want to do this: pattern="system /path1/file1 file1" new_pattern=" data /path2/file2 file2" file to edit: data.db - I need to search in the file data.db for the nth occurrence of pattern - pattern must... (14 Replies)
Discussion started by: Phil3759
14 Replies

7. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

8. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

9. Shell Programming and Scripting

AWK match $1 $2 pattern in file 1 to $1 $2 pattern in file2

Hi, I have 2 files that I have modified to basically match each other, however I want to determine what (if any) line in file 1 does not exist in file 2. I need to match column $1 and $2 as a single string in file1 to $1 and $2 in file2 as these two columns create a match. I'm stuck in an AWK... (9 Replies)
Discussion started by: right_coaster
9 Replies

10. Shell Programming and Scripting

Count the number of occurrences of a pattern between each occurrence of a different pattern

I need to count the number of occurrences of a pattern, say 'key', between each occurrence of a different pattern, say 'lu'. Here's a portion of the text I'm trying to parse: lu S1234L_149_m1_vg.6, part-att 1, vdp-att 1 p-reserver IID 0xdb registrations: key 4156 4353 0000 0000 ... (3 Replies)
Discussion started by: slipstream
3 Replies
Login or Register to Ask a Question