parse xm entry with awk/sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting parse xm entry with awk/sed
# 1  
Old 05-20-2011
parse xm entry with awk/sed

Hi folks,

I have XML files with the following sections (section occurs once per file) in them:
Code:
 <AuthorList CompleteYN="Y">
                <Author ValidYN="Y">
                    <LastName>Bernal</LastName>
                    <ForeName>Federico</ForeName>
                    <Initials>F</Initials>
                </Author>
                <Author ValidYN="Y">
                    <LastName>Tyler</LastName>
                    <ForeName>Andrew F</ForeName>
                    <Initials>AF</Initials>
                </Author>
                <Author ValidYN="Y">
                    <LastName>Korsmeyer</LastName>
                    <ForeName>Stanley J</ForeName>
                    <Initials>SJ</Initials>
                </Author>
                <Author ValidYN="Y">
                    <LastName>Walensky</LastName>
                    <ForeName>Loren D</ForeName>
                    <Initials>LD</Initials>
                </Author>
                <Author ValidYN="Y">
                    <LastName>Verdine</LastName>
                    <ForeName>Gregory L</ForeName>
                    <Initials>GL</Initials>
                </Author>
            </AuthorList>

I'm trying to incorporate an awk/sed function into a script which will print out the following output:
Code:
Federico Bernal, Andrew F Tyler, Stanley J Korsmeyer, Loren D Walensky, Gregory L Verdine

I've been experimenting with split and if/for loops in awk but can't seem to get first and last names on one line!

Thanks in advance for any help.
# 2  
Old 05-20-2011
Try:
Code:
perl -ne 'if (/<AuthorList/../<\/AuthorList/){$x=$1 if /<LastName>([^<]+)/;if (/<ForeName>([^<]+)/){print "$1 $x, "}}END {print "\n"}' file

# 3  
Old 05-20-2011
Code:
awk -F "[<>]" '/LastName/{ln=$3} /ForeName/{fn=$3} /Initials/ {s=fn "," $3 "," ln}END {print s}' file

# 4  
Old 05-21-2011
Code:
perl -ne 'printf "%s %s,",$1,$var if /<ForeName>(.+?)</; if(/<LastName>(.+?)</){$var=$1} END {print "\n"}' inputfile | sed 's/,$//'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/sed summation of one column based on some entry in first column

Hi All , I am having an input file as stated below Input file 6 ddk/djhdj/djhdj/Q 10 0.5 dhd/jdjd.djd.nd/QB 01 0.5 hdhd/jd/jd/jdj/Q 10 0.5 512 hd/hdh/gdh/Q 01 0.5 jdjd/jd/ud/j/QB 10 0.5 HD/jsj/djd/Q 01 0.5 71 hdh/jjd/dj/jd/Q 10 0.5 ... (5 Replies)
Discussion started by: kshitij
5 Replies

2. Shell Programming and Scripting

How to replace multiple "&nbsp;" entry with in <td> tag into single entry using sed?

I have the input file like this. Input file: 12.txt 1) There are one or more than one <tr> tags in same line. 2) Some tr tags may have one <td> or more tna one <td> tags within it. 3) Few <td> tags having "<td> &nbsp; </td>". Few having more than one "&nbsp;" entry in it. <tr> some td... (4 Replies)
Discussion started by: thomasraj87
4 Replies

3. Shell Programming and Scripting

Replacing specific entry using sed

Hi guys, I'm new to bash programming, so please pardon me. I'm trying to replace an entry's text in Books.txt This code works perfectly: sed -i "s/$BookTitle/$NewBookTitle/g" Books.txt But problem is, if there are double entries, it will also replace that entry. For example: ... (12 Replies)
Discussion started by: todaealas
12 Replies

4. Shell Programming and Scripting

sed/awk script to parse list of bandwidth rules

Hello all gurus, I have a long list of rules as below: 20 name:abc addr:203.45.247.247/255.255.255.255 WDW-THRESH:12 BW-OUT:10000000bps BW-IN:15000000bps STATSDEVICE:test247 STATS:Enabled (4447794/0) <IN OUT> 25 name:xyz160 addr:203.45.233.160/255.255.255.224 STATSDEVICE:test160... (3 Replies)
Discussion started by: sb245
3 Replies

5. Shell Programming and Scripting

Use awk or sed to parse delimited string

Hi I am trying to figure out the best way to search a long log file and print out certain information. For example if I had a line in a log file delimited by ampersand first_name=mike&last_name=smith&zip_code=55555&phone=555-5555&state=ma&city=boston and I only wanted to search for and... (3 Replies)
Discussion started by: mstefaniak
3 Replies

6. Shell Programming and Scripting

sed or awk to parse this text

I am just beginning with sed and awk and understand that they are "per" line input. That is, they operate on each line individually, and output based on rules, etc. But I have multi-line text blocks that looks as follows, and wish to ONLY extract the text between the first hyphen (-) and the... (13 Replies)
Discussion started by: bulgin
13 Replies

7. Shell Programming and Scripting

awk/sed Command: To Parse Stament between 2 numbers

Hi, I need an awk command that would parse the below expression Input Format 1 'Stmt1 ............................'2 'Stmt2 ............................'3 'Stmt3 ............................'4 'Stmt4 ............................'5 'Stmt5 ............................'6 'Stmt6... (1 Reply)
Discussion started by: rajan_san
1 Replies

8. Shell Programming and Scripting

awk/sed Command : Parse parameter file / send the lines to the ksh export command

Sorry for the duplicate thread this one is similar to the one in https://www.unix.com/shell-programming-scripting/88132-awk-sed-script-read-values-parameter-files.html#post302255121 Since there were no responses on the parent thread since it got resolved partially i thought to open the new... (4 Replies)
Discussion started by: rajan_san
4 Replies

9. Shell Programming and Scripting

To parse through the file and print output using awk or sed script

suppose if u have a file like that Hen ABCCSGSGSGJJJJK 15 Cock ABCCSGGGSGIJJJL 15 * * * * * * : * * * . * * * : Hen CFCDFCSDFCDERTF 30 Cock CHCDFCSDHCDEGFI 30 * . * * * * * * * : * * :* : : . The output shud be where there is : and . It shud... (4 Replies)
Discussion started by: cdfd123
4 Replies

10. UNIX for Dummies Questions & Answers

Muilti Entry Sed

My defaul shell is /bin/ksh I want to make multi entry sed changes to files mainly because is easier for me to check my arguments in that format, than all in just one line. example sed ' > s/this/for this/ > s/that/for that/ > s/I/me/' filex > filez Searching the net I read... (2 Replies)
Discussion started by: tony3101
2 Replies
Login or Register to Ask a Question