How to fetch the value from a xml using sed, GREP?


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
How to fetch the value from a xml using sed, GREP?

I have a simple xml file,need the output with the <value> tag and <result> tag

Code:
text.xml
<test-method status="FAIL" duration="45">
<value>
Id=C18
</value>
<result>
wrong paramter
</result>
</test-method>
<test-method status="FAIL" duration="45">
<value>
Id=C19
</value>
<result>
Data Issue
</result>
</test-method>
<test-method status="FAIL" duration="5">
<value>
Id=C20
</value>
<result>
mismtach
</result>
</test-method>
Output:
id=C18 wrong parameter
id=C19 data issue
id=C20 mismatch

Thanks

Last edited by DevAakash; 1 Week Ago at 02:31 PM..
# 2  
Code:
sed -n '/<value>\|<result>/ {n;p}' file

--- Post updated at 23:34 ---

Code:
awk '/<value>/ {getline tmp}; /<result>/ {getline; print tmp,$0}' file

--- Post updated at 23:38 ---

Code:
sed -n '/<value>/ {n;h}; /<result>/ {n;H;x;s/\n/ /;p}' file

--- Post updated at 23:43 ---

Code:
sed -n '/<value>/ {n;h}; /<result>/! b; n;H;x;s/\n/ /p' file

These 2 Users Gave Thanks to nezabudka For This Post:
# 3  
Thanks nezabudka . my <results> has multiple lines, only the first line gets displayed when using the command sed -n '/<value>/ {n;h}; /<result>/ {n;H;x;s/\n/ /;p}' file
# 4  
Code:
sed -n '/<value>/ {n;h}; /<result>/ {:1;n;/<\/result>/! {H;b1};x;s/\n/ /g;p}'

--- Post updated at 09:07 ---

Code:
sed -n '/<value>/ {n;h}; /<result>/! b; :1;n;/<\/result>/! {H;b1};x;s/\n/ /g;p'

This User Gave Thanks to nezabudka For This Post:
# 5  
Code:
awk '{gsub(/(\s<.*>)?\s/, " ")} !(NR%2)' RS='<value>|<\\/result>' file

# 6  
Create xm1

Code:
while read a
do
   read b
   echo $a $b
done

Code:
grep -v "<" text.xml |grep -v ">" |./xm1

This User Gave Thanks to jgt For This Post:
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Getting string from xml tags using sed and grep
umen
how to get string from tags using sed and grep i try to capture the tags : <groupId>org.test.proj.assent</groupId> <artifactId>mainapp</artifactId> <version>mainapp.1.4</version> <packaging>pom</packaging> <name>main app 1</name> and then from there i guess i will...... Shell Programming and Scripting
7
Shell Programming and Scripting
How to grep for a word in xml?
mohtashims
Hi, I have the below tag/s in my xml. <foreign-server name="MOHTASHIM_SERVER"> What will be the easist way to extract MOHTASHIM_SERVER without the double quotes "" from the above tag? Desired Output:... Shell Programming and Scripting
10
Shell Programming and Scripting
fetch last line no form file which is match with specific pattern by grep command
Himanshu_soni
Hi i have a file which have a pattern like this Nov 10 session closed Nov 10 Nov 9 08:14:27 EST5EDT 2010 on tty . Nov 10 Oct 19 02:14:21 EST5EDT 2010 on pts/tk . Nov 10 afrtetryytr Nov 10 session closed Nov 10 Nov 10 03:21:04 EST5EDT 2010 Dec 8 Nov 10 05:03:02 EST5EDT 2010 ...... Shell Programming and Scripting
13
Shell Programming and Scripting
Grep and fetch subsequent lines also
prvnrk
Hi, I need to grep a pattern and fetch subsequent lines till end of the data-set. E.g., i have a file like: AA 1111 23 34 BB 45 56 78 CC 22 44 AA 2222 78 34 56 BB 22 56 67 68 23 CC 56 78 DD 33 55 77 AA 3333 46 BB 58 79 In above file i have 3-data sets where each set starts with...... Shell Programming and Scripting
6
Shell Programming and Scripting
get rid of xml comment by grep or sed
RonLii
Hi, I would like to get rid of all comment in an xml file by grep or sed command: The content seem like this: <!-- ab cd ef gh ij kl --> Anyone can help? Thanks and Regards... Shell Programming and Scripting
3
Shell Programming and Scripting