Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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:
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.