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
# 15  
Old 12-27-2010
Well, PERL is more a language than a script, although some use it more to call executables than PERL libs! It has a pretty unique place, being as full featured as C/C++/JAVA but more script-interpret-like than JAVA, where you start worrying about heap space. I just jump all the way back and forth from ksh to C/C++/JAVA without stopping in the middle. Some I know live mostly in the middle happily enough.

The REGEX people seem to have moved in a PERL direction, to the point of introducing incompatibilities, and you now have to test which era you are coding in! Is it '\b', \<' or '(^|[^a-zA-Z0-9_])' for the left side of a word or identifier?
# 16  
Old 12-28-2010
ksh93 solution using regular expression with backreference:
Code:
re='(<name>)(?*)(</name>)'

while read line
do
   [[ $line == ${re} ]] && print ${line/${re}/\2}
done < infile

This User Gave Thanks to fpmurphy For This Post:
# 17  
Old 12-28-2010
Does that ksh92 bit work for 2+ name elements on the same line?

Usually, you have to trim the left one you find off the line and then reevaluate the rest of the line.
# 18  
Old 12-28-2010
@fpmurphy: I tried your ksh script, and it did not work running version "sh (AT&T Research) 93t+ 2010-06-21". Can you enlighten me as to what I did wrong?
# 19  
Old 12-29-2010
I used dtksh, which is a ksh 93 derivitive, and got a blank line:
Code:
$ echo '<name>ho</name><name>ha</name>' >infile
$ while read line
do
   [[ $line == ${re} ]] && print ${line/${re}/\2}
done < infile
 
$

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