LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-12-2021, 03:26 AM   #1
dilip_d21
LQ Newbie
 
Registered: Nov 2021
Posts: 14

Rep: Reputation: Disabled
Sed help


Hallo Team , Need a small help in sed 4.7

cat pom.xml

<artifactId>XXX-restapi-commons</artifactId> (line 10)
<version>1.0.88</version> (line 11)

[code]

sed -i \'/<artifactId>XXX-restapi-commons<\\/artifactId>/!b;n;c<version>\'$a\'</version>\' pom.xml

artifactId: No such file or directory

[code]




The above command works fine in sed 4.4 and the same is not working in 4.7 ubuntu (can some one point what is the mistake here ??

I just want to replace what ever version in line11 with the value in the variable a .

Thanks in advance

Last edited by dilip_d21; 11-12-2021 at 04:05 AM.
 
Old 11-12-2021, 04:01 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
please use code tags (and edit your post).
Also would be nice to explain what's happened with sed 4.7.
 
Old 11-12-2021, 04:05 AM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
Quote:
Originally Posted by dilip_d21 View Post
I just want to replace what ever version in line11 with the value in the variable a .
Then you'll want a different tool than sed, as it is no good at parsing XML. Try xmlstarlet instead. Or else write a short Python or Perl script. Either way, you need a proper XML parser for the task.
 
Old 11-12-2021, 04:08 AM   #4
dilip_d21
LQ Newbie
 
Registered: Nov 2021
Posts: 14

Original Poster
Rep: Reputation: Disabled
@pan64 , i have updated the error in the question itself , thanks
 
Old 11-12-2021, 04:11 AM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
You've escaped the single quotes. Leave the plain and the string "artifactId" won't get treated like a redirected file name. Again though sed is not the right tool for this job.

Which distro is this on, including version?
 
Old 11-12-2021, 04:18 AM   #6
dilip_d21
LQ Newbie
 
Registered: Nov 2021
Posts: 14

Original Poster
Rep: Reputation: Disabled
@Turbocapitalist , thanks i am not unfortunately good in python/perl ,

NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.3 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
Release: 20.04
Codename: focal

Last edited by dilip_d21; 11-12-2021 at 04:32 AM.
 
Old 11-12-2021, 04:27 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
Quote:
Originally Posted by dilip_d21 View Post
@pan64 , i have updated the error in the question itself , thanks
that is definitely not related to sed version change. As it was explained the syntax of that line is incorrect.
 
Old 11-12-2021, 04:33 AM   #8
dilip_d21
LQ Newbie
 
Registered: Nov 2021
Posts: 14

Original Poster
Rep: Reputation: Disabled
@pan64 , that sed command is working in Linux sed 4.4 and i am trying to migrate the code to ubuntu seb 4.7 and i face the issue , thanks
 
Old 11-12-2021, 04:44 AM   #9
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
I don't know what shell you were using in the Linux you're trying to migrate the code from, but in Bash the quoting is just plain wrong. This is not a sed issue.
Code:
$ cat pom.xml.part
<artifactId>XXX-restapi-commons</artifactId>
<version>1.0.88</version>
$ sed '\|<artifactId>XXX-restapi-commons</artifactId>|!b;n;c<version>'"$(sed --version|head -1)"'</version>' pom.xml.part
<artifactId>XXX-restapi-commons</artifactId>
<version>sed (GNU sed) 4.8</version>
This post shows how to do it with xmlstarlet.
Code:
$ cat pom.xml.part
<dependency>
  <artifactId>XXX-restapi-commons</artifactId>
  <version>1.0.88</version>
  <scope>test</scope>
</dependency>
$ xmlstarlet ed -u "//artifactId[text()='XXX-restapi-commons']/following-sibling::version" -v NEW pom.xml.part
<?xml version="1.0"?>
<dependency>
  <artifactId>XXX-restapi-commons</artifactId>
  <version>NEW</version>
  <scope>test</scope>
</dependency>

Last edited by shruggy; 11-12-2021 at 05:11 AM.
 
2 members found this post helpful.
Old 11-12-2021, 05:16 AM   #10
dilip_d21
LQ Newbie
 
Registered: Nov 2021
Posts: 14

Original Poster
Rep: Reputation: Disabled
Thanks shruggy , that worked , it just saw the mistake in the statement , thanks @Turbocapitalist and @pan64.

have a nice day , Cheers
 
Old 11-12-2021, 05:19 AM   #11
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
No problem. However, do take a good look at the xmlstarlet tool show in post #9, at least at its use of XPaths. Knowledge of XPaths is a highly portable skill and can be used for processing XML even in your own Python or Perl scripts. regex is the wrong tool here. XPath is one of the suitable tools.
 
Old 11-12-2021, 05:22 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
Quote:
Originally Posted by dilip_d21 View Post
@pan64 , that sed command is working in Linux sed 4.4 and i am trying to migrate the code to ubuntu seb 4.7 and i face the issue , thanks
again, the error message you posted:
Quote:
artifactId: No such file or directory
is not related to any sed version, that is a simple syntax error.

From the other hand it looks like your question has been answered, in that case please mark the thread as solved.
 
Old 11-12-2021, 05:36 AM   #13
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
To expand on what Turbocapitalist says. The problem with your code is that it assumes a rigid structure of input data which XML isn't. While the same xmlstarlet command can easily handle situations where it fed an equivalent, but differently formatted pom.xml. E.g. when order of <scope> and <version> is reversed:
Code:
$ cat pom.xml.part
<dependency>
  <artifactId>XXX-restapi-commons</artifactId>
  <scope>test</scope>
  <version>1.0.88</version>
</dependency>
$ xmlstarlet ed -u "//artifactId[text()='XXX-restapi-commons']/following-sibling::version" -v NEW pom.xml.part
<?xml version="1.0"?>
<dependency>
  <artifactId>XXX-restapi-commons</artifactId>
  <scope>test</scope>
  <version>NEW</version>
</dependency>
Or when <artifactId> and <version> are on the same line:
Code:
$ cat pom.xml.part
<dependency>
  <artifactId>XXX-restapi-commons</artifactId><version>1.0.88</version>
  <scope>test</scope>
</dependency>
$ xmlstarlet ed -u "//artifactId[text()='XXX-restapi-commons']/following-sibling::version" -v NEW pom.xml.part
<?xml version="1.0"?>
<dependency>
  <artifactId>XXX-restapi-commons</artifactId>
  <version>NEW</version>
  <scope>test</scope>
</dependency>
and so on.
 
Old 11-12-2021, 05:47 AM   #14
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,153

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
All that said, a properly constructed (simple) ERE sed would do just fine here. I must admit I would do that rather than faff around with trying to relearn xmlstarlet when I need it maybe once a year.

Each to their own ... :shrug:
 
Old 11-12-2021, 07:05 AM   #15
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
Code:
sed -i '
# sed code embedded in a multi-lined string
  \#<artifactId>XXX-restapi-commons</artifactId>#!b
  n; c<version>'"$a"'</version>
' pom.xml
In the shell a 'string in strong quotes' prevents all kind of substitutions.
Only the ' itself need to be escaped as '\'' where the first ' ends the string, followed by a \' outside, followed by a ' that starts another string.
Likewise
'"$a"'
where the first ' ends the string, followed by $a in "quotes" (that allows a $expression substitution only), followed by a ' that starts another string.

The sed code makes the assumption that version is always on the next line. An xml parser is safer.
 
  


Reply

Tags
quoting, xml



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed help to run sed command against multiple different file names bkone Programming 2 04-16-2012 12:27 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration