Awk/Nawk Questions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk/Nawk Questions
# 1  
Old 01-11-2012
Awk/Nawk Questions

Hi Guys,

This is the Input:

Code:
 
<xn:MeContext id="XXX012">
    <xn:ManagedElement id="1">
        <xn:attributes>
            <xn:userLabel>XXX012</xn:userLabel>
            <xn:swVersion>R58E68</xn:swVersion>
        </xn:attributes>
    </xn:ManagedElement>
</xn:MeContext>
<xn:MeContext id="XXX235">
    <xn:ManagedElement id="1">
        <xn:attributes>
            <xn:userLabel>XXX235</xn:userLabel>
            <xn:swVersion>R35X57</xn:swVersion>
        </xn:attributes>
    </xn:ManagedElement>
</xn:MeContext>

I want this as the output:

Code:
 
XXX012     R58E68
XXX235     R35X57

In other words I want to have two columns the first one for the userLabel and the second one for the swVersion. But the swVersion associated to a specific userLabel should be put in front of it. that mean the line right below the userLabel line should be greped for the swVersion and put in front of that userLabel.

Note: I underlined the userLabel and swVersion lines, the actual input does not have the lines underlined.

Thanks.
# 2  
Old 01-11-2012
Code:
$ nawk -F"[<>]" '/userLabel/{a=$3} /swVersion/ {print a,$3}' test.txt
XXX012 R58E68
XXX235 R35X57

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 01-11-2012
wow that was quick man thanks,

But one more question I have 2000+ useLabels does the command gaurantee that the correct swVersion (the one below it) is put in fron of the userLabel?
# 4  
Old 01-11-2012
we need to make sure we have the below pattern in the file.

Code:
 
userLabel
swVersion
userLabel
swVersion
userLabel
swVersion
userLabel
swVersion

if any one is missing, then we get a chance to miss the label or version.

make sure your xml has both the label and swVersion
# 5  
Old 01-11-2012
With XML gawk:
Code:
xgawk -lxml 'XMLPATH ~ /xn:userLabel/ && NF {
  ul = $0
  }
XMLPATH ~ /xn:swVersion/ && NF {
  print ul, $0
  }' infile.xml

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Why does awk fail when nawk does not?

bash-3.2$ /usr/bin/nawk '/<server>/{A=1;++i} A{print >> ("manages"i".tmp")} /<\/server>/{A=0}' config.xml bash-3.2$ /usr/bin/awk '/<server>/{A=1;++i} A{print >> ("manages"i".tmp")} /<\/server>/{A=0}' config.xml awk: syntax error near line 1 awk: bailing out near line 1 bash-3.2$ uname -a... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

Awk,nawk Help please

Hi Guys, I am in need of some help; I have an xml message file which contains personal details as shown below: , message=, message=, message=, message=, message=, message= I want to use nawk to parse these xml messages but I am new to awk and nawk. What I want is to get output... (7 Replies)
Discussion started by: James_Owen
7 Replies

3. Shell Programming and Scripting

awk or nawk in ksh

I am trying to use either awk or nawk in ksh88 to grep the word "Reason" in multiple files and than print the lines that say "Reason" in a particular format that is different from how they would normally print. The original input is as follows: ... (10 Replies)
Discussion started by: ther2000
10 Replies

4. UNIX for Dummies Questions & Answers

Help me to know about awk and nawk

Hi everyone, i am new to unix , so i want to know what is the use of awk and nawk. because in most of the place this cmds were used. so, if anyone provied the basic idea of this cmds, it will be much helpfull for me . . .. Thnks in Advance :) (9 Replies)
Discussion started by: natraj005
9 Replies

5. Shell Programming and Scripting

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies

6. UNIX for Dummies Questions & Answers

How to use awk instead of nawk?

Hi all, I can run the following script using nawk..However, I find that teh server dun support nawk.. May I know how to change teh script to use awk such that it will work? Very urgent.. thx! nawk 'BEGIN {FS=OFS=","} NR==FNR{arr=$2;next} $0 !~ "Documentation"{print $0;next} ... (2 Replies)
Discussion started by: kinmak
2 Replies

7. UNIX for Advanced & Expert Users

nawk & awk

###----------------------TEST FOR $ Value------------------------ sort $RTF 2>>$LOG | nawk -F\| '\ { for( i=1; i<=NF; ) { if( i == NF ) { amt=substr($i,1,1) #$ value if (amt == /^ * /) printf( "$%s\n", $i ); ... (5 Replies)
Discussion started by: sd12
5 Replies

8. Shell Programming and Scripting

nawk -v to awk

hi, i have the command nawk -v i want to use it equivalent in awk? any help please :) (2 Replies)
Discussion started by: kamel.seg
2 Replies

9. UNIX for Dummies Questions & Answers

help with Awk or nawk

Can anyone explain to me why the first line doesn't work and the second seems to work fine. I am trying to find all occurances of text within a certain column (col 13) that start with the character V, I suppose it sounds simple but I have tried using the following but don't really understand what... (2 Replies)
Discussion started by: Gerry405
2 Replies
Login or Register to Ask a Question