bash extract all occurences delimited from <name> and </name> tags from an xml file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting bash extract all occurences delimited from <name> and </name> tags from an xml file
# 1  
Old 12-26-2010
bash extract all occurences delimited from <name> and </name> tags from an xml file

I need to extract all text delimited from <name> and </name> tags from an xml file, but not only first occurence. I need to extract all occurences.
I've tried with this command:
Code:
awk -F"<name>|</name>" 'NF>2{print $2}'

but it give only first occurence. How can i modify it?

Last edited by Scott; 12-26-2010 at 01:45 PM.. Reason: Please use code tags
# 2  
Old 12-26-2010
Tell awk the record separator is < so every tag is a different record to be filtered.
# 3  
Old 12-26-2010
Quote:
Originally Posted by DGPickett
Tell awk the record separator is < so every tag is a different record to be filtered.
How i can do that? I'm not an expert with awk. If i use < as separator it identify also other tag and not only the text delimited by <name> </name> tags
# 4  
Old 12-26-2010
Hi,
something like this,
Code:
sed 's/<name>/ /g;s/<\\name>/ /g'  inputfile

# 5  
Old 12-26-2010
I've applied your suggested command to my xml file, but it doesn't show me only text between <name></name> tags. It shows the entire file.
I've applied it to this file h_tp://dl.dropbox.com/u/877248/info.xml
I need to obtain only a list.
# 6  
Old 12-26-2010
Not very 'clean but working (with your sample) one:
Code:
sed -e 's/name>/\n/g' info.xml | grep '^[^<]*<\/' | cut -d'<' -f1

result in the attachement
# 7  
Old 12-26-2010
If you want to do it with awk, try this:
Code:
awk '!/<.*>/' RS="<name>|</name>" file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract the specific tags in a XML file

Hello Shell Gurus, I have a requirement to get the specific tags from a XML file. Here is my code snippet <jdbc-system-resource> <name>SDPData Source</name> <target>AdminServer,osb_server1,soa_server1</target> ... (30 Replies)
Discussion started by: Siv51427882
30 Replies

2. Shell Programming and Scripting

Extract a particular xml only from an xml jar file

Hi..need help on how to extract a particular xml file only from an xml jar file... thanks! (2 Replies)
Discussion started by: qwerty000
2 Replies

3. UNIX for Dummies Questions & Answers

awk - Extract 4 lines in Column to Rows Tab Delimited between tags

I have tried the following to no avail. xargs -n8 < test.txt awk '{if(NR%6!=0){p=""}else{p="\n"};printf $0" "p}' Mod_Alm_log.txt > test.txt I have tried different variations of the above, the problem is mixes lines together. And it includes the tags "%a and %A" I need them to be all tab... (16 Replies)
Discussion started by: mytouchsr
16 Replies

4. Shell Programming and Scripting

Shell script to extract data in repeating tags from xml

Hi, I am new to shell scripting. I need to extract data between repeating tags from an xml file and store the data in an array to process it further. <ns1:root xmlns:ns1="http://example.com/config"> <ns1:interface>in1</ns1:interface> <ns1:operation attribute1="true" attribute2="abd"... (2 Replies)
Discussion started by: sailendra
2 Replies

5. Shell Programming and Scripting

Extract a nth field from a comma delimited file

Hi, In my file (which is "," delimited and text qualifier is "), I have to extract a particualr field. file1: 1,"aa,b",4 expected is the 2nd field: aa,b I tried the basic cut -d "," -f 2 file 1, this gave me aa alone instead aa,b. A small hint ot help on this will be very... (5 Replies)
Discussion started by: machomaddy
5 Replies

6. Shell Programming and Scripting

Extract second column tab delimited file

I have a file which looks like this: 73450 articles and news developmental psychology 2006-03-30 16:22:40 1 http://www.usnews.com 73450 articles and news developmental psychology 2006-03-30 16:22:40 2 http://www.apa.org 73450 articles and news developmental psychology 2006-03-30... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

7. UNIX for Dummies Questions & Answers

Extract records by column value - file non-delimited

the data in my file is has no delimiters. it looks like this: H52082320024740010PH333200612290000930 0.0020080131 D5208232002474000120070306200703060580T1502 TT 1.00 H52082320029180003PH333200702150001 30 100.0020080205 D5208232002918000120070726200707260580T1502 ... (3 Replies)
Discussion started by: jclanc8
3 Replies

8. UNIX for Dummies Questions & Answers

Extract a specific number from an XML file based on the start and end tags

Hello People, I have the following contents in an XML file ........... ........... .......... ........... <Details = "Sample Details"> <Name>Bob</Name> <Age>34</Age> <Address>CA</Address> <ContactNumber>1234</ContactNumber> </Details> ........... ............. .............. (4 Replies)
Discussion started by: sushant172
4 Replies

9. UNIX for Dummies Questions & Answers

How to count the occurences of a specific word in a file in bash shell

Hello, I want to count the occurences of a specific word in a .txt file in bash shell. Can somebody help me pleaze?? Thanks!!! (2 Replies)
Discussion started by: mskart
2 Replies

10. Shell Programming and Scripting

how to extract a tilde delimited file in unix

i have a file in unix in which datas are like this 07 01 abc data entry Z3 data entry ASSISTANT Z3 39 08 01 POD peadiatrist Z4 POD PeDIATRY Z4 67 01 operator specialist 00 operator UNSPECIFIED A0 00 ... (12 Replies)
Discussion started by: trichyselva
12 Replies
Login or Register to Ask a Question