How to grep for a word in xml?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grep for a word in xml?
# 8  
Old 05-23-2016
I don't think you need the length function for a string constant, and the match should be in the pattern, not in the action part, to reliably eliminate lines without the search string:
Code:
awk 'match($0,/<foreign-server name=\".*\"/) {print substr($0,RSTART+22,RLENGTH-23)}' file
MOHTASHIM_SERVER

# 9  
Old 05-23-2016
Quote:
Originally Posted by RudiC
I don't think you need the length function for a string constant, and the match should be in the pattern, not in the action part, to reliably eliminate lines without the search string:
Code:
awk 'match($0,/<foreign-server name=\".*\"/) {print substr($0,RSTART+22,RLENGTH-23)}' file
MOHTASHIM_SERVER

Thank you Rudi, I have put length inside match because I don't want to hardcode values inside the code eg-->+22and-23 so putting that we could look for any string and it's length will take care of that part was my thinking on it. Please do correct me if I am wrong here.

Thanks,
R. Singh
# 10  
Old 05-23-2016
mohtashims,
Assuming that there is no more than one "foreign-server" tag on each line in your input file, you might also try this slight modification to RavinderSingh13's code:
Code:
awk '
BEGIN {	LEN1 = length("<foreign-server name=\"")
	LEN2 = length("\">")
}
match($0, /<foreign-server name="[^"]*">/) {
	print substr($0, RSTART + LEN1, RLENGTH - LEN1 - LEN2)
}'  Input_file

which should also work if other text containing quotes follows the "foreign-server" tag on lines containing that tag.

Neither of these suggestions will work if more than one "foreign-server" tag appears on a single line, but they will fail in different ways.

If someone wants to try either of these suggestions on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.

RudiC,
I agree that the BEGIN section (and the LEN1 and LEN2 variables) aren't needed, but they might make it easier for someone less experienced to understand the calculations going on in the substring operation.

Ravinder,
Note that when an ERE is delimited by / characters, " characters in the ERE don't need to be escaped.
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 05-24-2016
As the length of this thread has shown, handling XML is not trivial, not unless its very simple and regular XML. Also, posting "prettier" data than your real input is liable to get non-working solutions because it's much easier to write for pretty XML than ugly XML.

If we throw out "simple", you can use my yanx.awk library like:
Code:
awk -f yanx.awk -e 'TAG=="FOREIGN-NAME" { print ARGS["NAME"];  delete ARGS["NAME"]; }' ORS="\n" filename

Use nawk on solaris.

If this doesn't work, please show your actual input so I can figure out why.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep for a word or word with underscore

I have a file "test" with following contents: cat test abc abcd_efg abc_abc I want to only grep for abc or abc_ without getting other results, how do I achieve this? If I use grep -w abc test option I get only abc and not abc_. If I use egrep "abc|abc_" test its still printing... (3 Replies)
Discussion started by: ctrld
3 Replies

2. Shell Programming and Scripting

Replacing a particular word with another word in all the xml's under a particular directory with sed

Hi Folks, Could you please advise what will be the SED command to replace a word in all xml's under a particular directory for example let say I rite now at the following below location $ cd /ter/rap/config now under config directory there will be lots of xml file , now my objective is to... (1 Reply)
Discussion started by: punpun66
1 Replies

3. Shell Programming and Scripting

Need a word which just comes next to after grep of a specific word

Hi, Below is an example : ST1 PREF: int1 AVAIL: int2 ST2 PREF :int1 AVAIL: int2 I need int1 to come in preferred variable while programming and int2 in available variable Please help me doing so Best regards, Vishal (10 Replies)
Discussion started by: Vishal_dba
10 Replies

4. Shell Programming and Scripting

Extract part of word from XML

Hi All, Can Someone help me in capturing a word from xml Using sed or awk or any other way in unix. i have file abc.xml like this <?xml version="1.0" encoding="ISO-8859-1" standalone="no" ?> - <NREC xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> - <HEADER> <SOURCE>MAIL</SOURCE>... (5 Replies)
Discussion started by: naveenkumarc
5 Replies

5. Shell Programming and Scripting

How ti Grep for a word and print the next word

Hi can we grep for a word and print the next word of the greped word? ex:- create or replace function function_name create function function_name we should search for word "function" and output next word "function_name" from both lines. (3 Replies)
Discussion started by: manasa_vs
3 Replies

6. Shell Programming and Scripting

grep part of word or Another word from a string

Hi all, FileOne family balance >>>>> 0 0 0 0 java.io.FileNotFoundException: Settings.xml (No such file or directory) at java.io.FileInputStream.open(Native Method) .. .... ..... ..... java.lang.NullPointerException ... ..... ...... Stacktrace: at... (2 Replies)
Discussion started by: linuxadmin
2 Replies

7. Shell Programming and Scripting

How to grep a word and next column to that word?

Hi, I have input file as below. Can you help me? inac_4y;0;2;Balance;200;1;1; 0;2;Balance;100;1; 0;inac_nq;0;1;Balance;100;1 desired output Balance;200 Balance;100 Balance;100 -Suresh Please use and tags when posting code, data or logs etc. to preserve formatting... (5 Replies)
Discussion started by: suresh3566
5 Replies

8. Shell Programming and Scripting

Grep out specific word and only that word

ok, so this is proving to be kind of difficult even though it should not be. say for instance I want to grep out ONLY the word fkafal from the below output, how do I do it? echo ajfjf fjfjf iafjga fkafal foeref afoafahfia | grep -w "fkafal" If i run the above command, i get back all the... (4 Replies)
Discussion started by: SkySmart
4 Replies

9. UNIX for Dummies Questions & Answers

how to grep the word and display only the second word from it

hi, consider the below line in a text file, 'Y',getdate(),'N','V',NULL ..... 'N',getdate(),'Y','D',NULL ..... 'Y','N','Y',getdate(),'Y','D',NULL .... as u see above, i want only the second word after the getdate() word... getdate() will not come 2nd word alwys it may be any position but i... (11 Replies)
Discussion started by: prsam
11 Replies

10. UNIX for Dummies Questions & Answers

how to grep for a word and display only the word

Hi, When we "grep" for a word in a file, it returns the lines containing the word that we searched for. Is there a way to display only the words and not the entire line containing them. Thanks Ananth (6 Replies)
Discussion started by: ananthmm
6 Replies
Login or Register to Ask a Question