get xml note by awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting get xml note by awk
# 1  
Old 04-30-2012
get xml note by awk

Hi all,

I have a file contain 100 lines xml. Would like to get the note value of special attribute. Could anyone help?

input:
Code:
<a>1</a><b>2</b><c>3</c><d>4</d><e>5</e><f>64</f>
<a>1</a><b>2</b><c>33</c><d>4</d><e>56</e><f>63</f>
<a>1</a><b>2</b><c>66</c><d>4</d><e>58</e><f>62</f>
<a>1</a><b>2</b><c>77</c><d>4</d><e>59</e><f>61</f>

Code:
awk -f -v variable=c test.awk

output:
Code:
3
33
66
77

Code:
awk -f -v variable=f test.awk

output:
Code:
64
63
62
61


Last edited by Franklin52; 05-01-2012 at 03:53 AM.. Reason: Please use code tags
# 2  
Old 04-30-2012
Code:
[user@cygwin ~]$ cat test.sh
#! /bin/bash
sed "s/.*<$1>\([^<]*\)<\/$1>.*/\1/" inputfile.xml
[user@cygwin ~]$
[user@cygwin ~]$ ./test.sh c
3
33
66
77
[user@cygwin ~]$ ./test.sh f
64
63
62
61
[user@cygwin ~]$

This User Gave Thanks to balajesuri For This Post:
# 3  
Old 04-30-2012
Code:
 
$ nawk -F"[<>]" -v var="a" '{for(i=1;i<=NF-1;i++)if($i~var){print $(i+1);next}}' test.txt
1
1
1
1
$ nawk -F"[<>]" -v var="b" '{for(i=1;i<=NF-1;i++)if($i~var){print $(i+1);next}}' test.txt
2
2
2
2
$ nawk -F"[<>]" -v var="c" '{for(i=1;i<=NF-1;i++)if($i~var){print $(i+1);next}}' test.txt
3
33
66
77
$ cat test.txt
<a>1</a><b>2</b><c>3</c><d>4</d><e>5</e><f>64</f>
<a>1</a><b>2</b><c>33</c><d>4</d><e>56</e><f>63</f>
<a>1</a><b>2</b><c>66</c><d>4</d><e>58</e><f>62</f>
<a>1</a><b>2</b><c>77</c><d>4</d><e>59</e><f>61</f>

# 4  
Old 04-30-2012
Code:
awk '$1==var{print $2}' var=c RS=\< FS=\> infile

This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Note: environment variables within awk

This is not a question. Just a little note, because I've been here some time and never read about awk accessing environment variables. So here's my use case and demonstration of how to use the ENVIRON array. My operating environment is ubuntu 18.04 / docker / GNU awk 4.1.4. ENVIRON seems to posix... (2 Replies)
Discussion started by: stomp
2 Replies

2. Shell Programming and Scripting

XML Phase with awk

Hi Guys, Input XML File :- <managedObject class="RMOD_R" distName="MRBTS-101/X/R-7"> <list name="activeCellsList"> <p>15</p> <p>201</p> </list> <p name="aldManagementProtocol">True</p> <p name="serialNumber">845</p> </managedObject> Output :- ... (5 Replies)
Discussion started by: pareshkp
5 Replies

3. Shell Programming and Scripting

How to add Xml tags to an existing xml using shell or awk?

Hi , I have a below xml: <ns:Body> <ns:result> <Date Month="June" Day="Monday:/> </ns:result> </ns:Body> i have a lookup abc.txtt text file with below details Month June July August Day Monday Tuesday Wednesday I need a output xml with below tags <ns:Body> <ns:result>... (2 Replies)
Discussion started by: Nevergivup
2 Replies

4. Shell Programming and Scripting

XML Parsing using awk

Hi All, I have a problem to resolve. For following XML file, I need to parse the values based on Tag Name. I would prefer to use this by awk. I have used sed command to replace the tags (s/<SeqNo>//). In this case there can be new tags introduced. So need to parse it based on Tag Name. Any... (9 Replies)
Discussion started by: Tons
9 Replies

5. Shell Programming and Scripting

xml parsing with awk

hi all.. need your help again.. i have xml file and i want to parsing some data from the xml file.. <ex-name="keroco"> <................> <................> <................> <br-name="cincai"> <ship="123456"> <...................> ... (3 Replies)
Discussion started by: buncit8
3 Replies

6. Shell Programming and Scripting

processing xml with awk

With the following input sample extracted from a xml file <rel ver="123"> <mod name="on"> <node env="ac" env="1"> <ins ip="10.192.0.1"/> <ins ip="10.192.0.2"/> ... (1 Reply)
Discussion started by: cabrao
1 Replies

7. Shell Programming and Scripting

awk XML

ho un file xml vorrei fare due cose: eliminare tutto il testo prima del tag <prodottti> e dopo la fine del tag </prodotti> sostituire i caratteri cr e lf con blank Come fare ?? Grazie. (3 Replies)
Discussion started by: alice07
3 Replies

8. Shell Programming and Scripting

Read content between xml tags with awk, grep, awk or what ever...

Hello, I trying to extract text that is surrounded by xml-tags. I tried this cat tst.xml | egrep "<SERVER>.*</SERVER>" |sed -e "s/<SERVER>\(.*\)<\/SERVER>/\1/"|tr "|" " " which works perfect, if the start-tag and the end-tag are in the same line, e.g.: <tag1>Hello Linux-Users</tag1> ... (5 Replies)
Discussion started by: Sebi0815
5 Replies

9. Shell Programming and Scripting

parsing xml using awk

hello , i am trying to parse xml using awk however its a little bit tricky as i want <databases> <source> <host>prod</host> <port>1522</port> <tns>GP1</tns> <user>P11</user>... (6 Replies)
Discussion started by: amit1_x
6 Replies

10. Shell Programming and Scripting

XML Awk : Urgent

Hi All, I am a beginner to Unix. So would really appreciate if people out here can help me out. I have a XML file which has a element <NoteData> in which two values DBHA and DAKO. I need to search these in all the XML files in a directory and if found in the XML file then replace the... (3 Replies)
Discussion started by: karansachdeva
3 Replies
Login or Register to Ask a Question