Print a pattern between the xml tags based on a search pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print a pattern between the xml tags based on a search pattern
# 1  
Old 03-19-2011
Print a pattern between the xml tags based on a search pattern

Hi all,

I am trying to extract the values ( text between the xml tags) based on the Order Number.

here is the sample input
Code:
<?xml version="1.0" encoding="UTF-8"?>
<NJCustomer>
    <Header>
        <MessageIdentifier>Y504173382</MessageIdentifier>
        <ProcessIdentifier>253128</ProcessIdentifier>
        <MessageProducer>NJCustomer ERP ISF</MessageProducer>
        <MessageConsumer>NJ</MessageConsumer>
        <MessageFunction>ErpOrderNotification</MessageFunction>
        <MessageDateTime gmtOffset="-4">2011-03-18T06:01:43.209-04:00</MessageDateTime>
    </Header>
    <ErpOrderNotification type="Booked">
        <OrderNumber>939511</OrderNumber>
        <ErpOrderNumber>504173382</ErpOrderNumber>
        <ErpOrderStatus>Booked</ErpOrderStatus>
        <StatusChangeDateTime gmtOffset="-04:00">20110318 0601</StatusChangeDateTime>
        lOfServiceCode>LOCP</LevelOfServiceCode>
        <CarrierCode>LOCP</CarrierCode>
        <Location type="destination">
            <LocationCode>c/o Arvato Distribution GmbH</LocationCode>
        </Location>
    </ErpOrderNotification>
</NJCustomer>

I need to feed the order number (Here in this example 939511) and it should display the text between <NJCustomer> and </NJCustomer>.

The input file is very large and i feel a solution in awk could be better.

I searched the forum and i got this code and seems it needs small modification and i am not sure what this code does.


Code:
awk -v order=$ORD '/<NJCustomer>/{if(l)print s;l=0;s=$0;next}/order/{l=1}{s=s RS $0}END{if(l)print s}' <filename>

# 2  
Old 03-19-2011
For XML processing I'd prefer Perl:
Code:
perl -ln0e 'while (/<NJCustomer>.*?<\/NJCustomer>/gs){$x=$&;print $x if $x=~/<OrderNumber>939511<\/OrderNumber>/}' file

# 3  
Old 03-19-2011
Bartus...yes its works and difficult to understand the perl line. But how to change the order number as variable(mean for different order number)
# 4  
Old 03-19-2011
If order number is stored in shell variable ORD, then this will work:
Code:
perl -ln0e "while (/<NJCustomer>.*?<\/NJCustomer>/gs){\$x=\$&;print \$x if \$x=~/<OrderNumber>$ORD<\/OrderNumber>/}" file

This User Gave Thanks to bartus11 For This Post:
# 5  
Old 03-19-2011
Code:
awk -F'[<|>]' '$2 ~ "NJCustomer"{f=f?0:1}f && $2 =="OrderNumber"{print $3}' file

# 6  
Old 03-19-2011
Danmero,

Your solution reveals the order number only...but my query is if the order number is there in the file corresponding <NJCustomer> & </NJCustomer> needs to be printed
# 7  
Old 03-19-2011
Quote:
Originally Posted by oky
Danmero,

Your solution reveals the order number only...but my query is if the order number is there in the file corresponding <NJCustomer> & </NJCustomer> needs to be printed
Ups...Smilie
Code:
awk -F'[<|>]' '$2 ~ "NJCustomer"{f=f?0:1}f && $2 =="OrderNumber"{print $3}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

Print column based on pattern

Hi all, how print on columns when contain un pattern specific, e.g. $cat file1 3234 234 2323 number1 number2 number3 123 242 124 124 number2 324 424 543 626 number1 3463 234 534 345 number3 6756 345 2352 334 345 234 need output file1 way (2 Replies)
Discussion started by: aav1307
2 Replies

3. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

4. UNIX for Dummies Questions & Answers

Updating value based on search pattern

I have a file with following data <Field FieldName="CHCFA21_01_01" FieldType="Text"> <Output CapturedValue=""> <DataSource Name="" Value="" /> </Output> </Field> <Field FieldName="CHCFA21_01_02" FieldType="Date"> <Output CapturedValue=""> ... (1 Reply)
Discussion started by: nsuresh316
1 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

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

7. Shell Programming and Scripting

split XML file into multiple files based on pattern

Hello, I am using awk to split a file into multiple files using command: nawk '{ if ( $1 == "<process" ) { n=split($2, arr, "\""); file=arr } print > file }' processes.xml <process name="Process1.process"> ... (3 Replies)
Discussion started by: chiru_h
3 Replies

8. Shell Programming and Scripting

pattern search and print using sh

Hi All, Have a file contains multiple line with the following file.txt insert: akdkas job:ksdjf command: aldfasdf asdfsdfa asdfas.sh machine: asdfa need to grep for insert and machine and print only "akdkas,asdfa" cat file.txt | egrep "insert|machine" | awk -F: '{print $2}' output ... (5 Replies)
Discussion started by: uniqme
5 Replies

9. Shell Programming and Scripting

Search and replace - pattern-based

Hey folks! I am new to shell-scripting, but I have a problem that I would like to solve using a script. I create very large html forms, used for randomized trials. In these forms, each question is supplied with a variable that looks something like this: PROJECT_formNN Where NN is the question... (1 Reply)
Discussion started by: Roevhat
1 Replies

10. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies
Login or Register to Ask a Question