Getting string from xml tags using sed and grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting string from xml tags using sed and grep
# 1  
Old 02-04-2019
Getting string from xml tags using sed and grep

how to get string from tags using sed and grep
i try to capture the tags :
Code:
 <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 extract the string :

<version>mainapp.1.4</version>


i tried this :

Code:
 sed -n '/version/,/version/p' pom.xml | grep -o -e '<version>.*'

but it gives me all the versions

also i try to capture :

Code:
 sed -n '/\/artifactId/,/\/version/p' pom.xml | grep -o -e '<version>.*'

but all file is printed




Code:
  <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" >
    	<modelVersion>55.0.0</modelVersion>
     
    	<parent>
    		<groupId>org.test.proj</groupId>
    		<artifactId>test-invoker</artifactId>
    		<version>invoker.0.4</version>
    	</parent>
    
    	<groupId>org.test.proj.assent</groupId>
    	<artifactId>mainapp</artifactId>
    	<version>mainapp.1.4</version>
    	<packaging>pom</packaging>
    	<name>main app 1</name>
    
    	<properties>
    		<app-name>Testing App</app-name>
    	</properties>
    	
    	<dependencyManagement>
    		<dependencies>
    			<dependency>
    				<groupId>com.prod.db</groupId>
    				<artifactId>srver-db</artifactId>
    				<version>${project.version}</version>
    			</dependency>
    		</dependencies>
    	</dependencyManagement>
    	</project>


i can use only native linux tools no installs
# 2  
Old 02-04-2019
You GUESS you will extract a string?






EDIT: Try
Code:
sed -n '/<parent>/,/<\/parent>/b;/<dependencyManagement>/,/<\/dependencyManagement/b;/<version>/p' file
        <version>mainapp.1.4</version>

These 2 Users Gave Thanks to RudiC For This Post:
# 3  
Old 02-04-2019
Please see why this won't (reliably) work.

In your case you try to discern between <version>.....</version> inside and outside of certain other tags (like "parent" or "dependencyManagement"). This is called "context", so you do a context-oriented search. sed (and even more so grep) are simply not the right tools for this kind of endeavour. Use an XML parser for that, otherwise you always get results that are "more or less correct" - which, with bad circumstances, comes down to "outright wrong".

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 4  
Old 02-04-2019
Thanks allot , can you please explain your sed expression ?
# 5  
Old 02-05-2019
Well, not much to explain. I deployed the sed constructs that you already used in your code snippets in post #1. The b (branch) command jumps to script end if no label is given, skipping all remaining commands (c.f. man sed). I used it to avoid / eliminate the "parent" and "dependency..." sub tag groups. Then, print just the "version" line.
This proposal is taylored to exactly your sample file, and can't be used for a more generic problem, as bakunin pointed out already.
# 6  
Old 02-05-2019
There's also my general-purpose XML script.

Code:
awk -f yanx.awk -e 'TAG=="VERSION" { print $2 }' ORS="\n" groupid.xml
mainapp.1.4

$

# 7  
Old 02-05-2019
Just in case you'd like to have an xml-parser to parse an xml file...

xmlstarlet

Code:
  xmlstarlet sel -N my=http://maven.apache.org/POM/4.0.0 -t -v "//my:project/my:version" data.xml
 --> mainapp.1.4

xmllint


Code:
 xmllint --xpath "//*[local-name()='project']/*[local-name()='version']/text()" data.xml
--> mainapp.1.4

This is more complicated here, since a namespace is involved here.

Last edited by stomp; 02-05-2019 at 01:05 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

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 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... (5 Replies)
Discussion started by: DevAakash
5 Replies

2. Shell Programming and Scripting

Replace string in XML file with awk/sed with string from another

Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file: </member> <member> <name>TransactionID</name> <value><string>123456789123456</string></value> </member> <member> <name>Number</name> ... (9 Replies)
Discussion started by: cozzin
9 Replies

3. Shell Programming and Scripting

Search and replace the string with new word using xml tags

Hi All i need to replace the url1 inside <remote> tag in below xml in first instance and in the second instance with url2. any help appreciated <locations> <hudson.scm.SubversionSCM_-ModuleLocation> <remote>https://svn2015.com/svn/repos/internalshard</remote> ... (4 Replies)
Discussion started by: madankumar.t@hp
4 Replies

4. Shell Programming and Scripting

awk and or sed command to sum the value in repeating tags in a XML

I have a XML in which <Amt Ccy="EUR">3.1</Amt> tag repeats. This is under another tag <Main>. I need to sum all the values of <Amt Ccy=""> (Ccy may vary) coming under <Main> using awk and or sed command. can some help? Sample looks like below <root> <Main> ... (6 Replies)
Discussion started by: bk_12345
6 Replies

5. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

6. Shell Programming and Scripting

get rid of xml comment by grep or sed

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 (3 Replies)
Discussion started by: RonLii
3 Replies

7. Shell Programming and Scripting

Like grep -v for a string over 2 lines? Sed?

Hi, I have a log file that I need to monitor as it's being written to, and I want to exclude certain strings from the output. At the moment I'm using ... tail -f LogFileName_`date +%d`.log | egrep -v "First String To Exclude | 2nd string | 3rd string" ...which works OK - but now I need to... (1 Reply)
Discussion started by: jake657
1 Replies

8. Shell Programming and Scripting

replace string in XML with sed

Greetings, I have an XML : file.xml <component> <name>abcd</name> <value>1234</value> </component> I am using sed to replace abcd with the desired value dynamically without knowing the actual value. sed 's/<name>./]\{1,\}<\/name>/<name>ijkl<\/name>/' file.xml > newfile.xml I... (6 Replies)
Discussion started by: chiru_h
6 Replies

9. Shell Programming and Scripting

Grep XML tags

I want to search the below XML pattern in the XML files, but the XML files would be in a .GZ files, <PRODID>LCTO84876</PRODID> <PARTNUMBER>8872AC1</PARTNUMBER> <WWPRODID>MODEL84876</WWPRODID> <COUNTRY>US</COUNTRY> <LANGUAGE>1</LANGUAGE> What's the command/script to search it ? :confused: (2 Replies)
Discussion started by: saravvij
2 Replies

10. Shell Programming and Scripting

Grep xml tags

Hi I want to get the value between to XML tags as follows <EAN>12345</EAN> so i would want to return 12345. i have tried sed and awk but can't do it. can anyone help? (9 Replies)
Discussion started by: handak9
9 Replies
Login or Register to Ask a Question