LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-11-2020, 12:55 AM   #16
kropex
LQ Newbie
 
Registered: Apr 2020
Posts: 13

Rep: Reputation: 7

Quote:
Originally Posted by shruggy View Post
Glad you sorted it out. But sed is a wrong tool to parse XML. In your particular case, this really feels like
Then, to change the MAC address on the second interface, do
Code:
xmlstarlet ed -u "//interface[@type='client']/mac/@address" -v 52:54:00:8b:c9:43 myfile.xml
Interesting to know this for parsing xml, in my case I couldn't test this command because the command not found. But in your example is not a good response for starting subject of the thread where the request is "second" match. Also to my application is not useful because I have 4 mac addresses with same type and I need to generate one mac for each one.

Why do you consider that sed is not good for parsing xml? sed is good for any txt file, just that sed is more difficult to adapt for needs in case of parsing xml, i guess.

thank you,
 
Old 06-11-2020, 05:06 AM   #17
shruggy
Member
 
Registered: Mar 2020
Posts: 994

Rep: Reputation: Disabled
Quote:
Originally Posted by kropex View Post
in my case I couldn't test this command because the command not found.
Judging from your other thread, you're using CentOS. xmlstarlet for CentOS is provided by the EPEL repository.

Quote:
Originally Posted by kropex View Post
But in your example is not a good response for starting subject of the thread where the request is "second" match.
Because requesting an XML tag by its order is usually the wrong way if there are other means to get it.

xmlstarlet uses XPath however, so even if you cannot select an XML tag by attribute value, you can still always select it by order:
Code:
xmlstarlet ed -u "//interface[2]/mac/@address" -v 52:54:00:8b:c9:43 myfile.xml
I'm still curious, why the solutions suggested previously in this thread didn't work for you. I mean
Code:
awk -vq="'" -vm="00:11:22:33:44:55" '/mac/{c++;if(c==2)sub(/=.*/,"="q m q"/>")}1' myfile.xml
would replace the second MAC address in my example above just fine.

Quote:
Originally Posted by kropex View Post
Why do you consider that sed is not good for parsing xml?
XML is not a line-oriented format. Even if each MAC address happens to be on a line by itself in your case, there are no guarantees that the XML file won't be reformatted in the future, and then your carefully crafted sed expression will suddenly stop to work. Consider, e.g. what would happen with your script if the XML from my example above were formatted like this, with both mac and source addresses on the same line:
Code:
<mac address='52:54:00:8b:c9:51'/><source address='192.168.0.1' port='5558'/>
There are also no guarantees that XML tags on the same level will always stay in the same order. While less likely than just reformatting, the order of tags may change, too:
Code:
<source address='192.168.0.1' port='5558'/><mac address='52:54:00:8b:c9:51'/>
And don't forget that X in XML stands for eXtensible. There may be new tags added later:
Code:
<mac address="00:11:22:33:44:55"/>
<ip family='ipv4' address='172.17.2.0' prefix='24'/>
<ip family='ipv6' address='2001:db8:ac10:fd01::' prefix='64'/>
 
1 members found this post helpful.
Old 06-11-2020, 05:37 AM   #18
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,912

Rep: Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249
Man, I wish jeremy would lock down old threads.
 
Old 06-11-2020, 06:39 AM   #19
kropex
LQ Newbie
 
Registered: Apr 2020
Posts: 13

Rep: Reputation: 7
Unhappy

Quote:
Originally Posted by syg00 View Post
Man, I wish jeremy would lock down old threads.
Sometimes I wonder how people like you have so high reputation but beside this a wish to have a thread closed without good solution? In our last conversations I think we brought a lot of improves to this thread and I'm happy that I had this discuss.
Thank you to all for help, and your other solutions are also very good, I note them down to use it at needed moment.
 
Old 06-11-2020, 06:44 AM   #20
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,912

Rep: Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249Reputation: 3249
New threads using new/updated tools referring back to old threads are useful and appropriate. Introducing new solutions to an old thread that were unavailable at the time of the initial query are confusing to later searchers.
Times move on.
 
Old 06-11-2020, 12:30 PM   #21
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,409
Blog Entries: 11

Rep: Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359
Indeed, this is a bit of an old thread to wake without a solid reason. Although new relevant information may always be useful to others, waking old threads to add new variations of the methods already covered often adds confusion.

@kropex - if you have your own related problem to solve it is always best to begin your own new thread with the details specific to your case, perhaps including a reference to previous discussions with an explanation of why they do or do not apply to your case.

I agree that sed is usually not the right tool to use for parsing XML (xmlstarlet is a good choice!) as has been pointed out, but discussion of more appropriate XPath based solutions would be better continued in a dedicated thread with a current specific example.

Lets let this thread go back to sleep...
 
Old 06-12-2020, 02:34 PM   #22
kropex
LQ Newbie
 
Registered: Apr 2020
Posts: 13

Rep: Reputation: 7
Quote:
Originally Posted by astrogeek View Post
@kropex - if you have your own related problem to solve it is always best to begin your own new thread with the details specific to your case, perhaps including a reference to previous discussions with an explanation of why they do or do not apply to your case.
I just know one important rule of forums. Search first and if you find a thread closed to your desire do not open a new thread on the same subject. The request for this thread, start of the thread was exactly the problem I wanted to solve for me, but in this thread I couldn't find the solution, so I started to continue to have finally a good solution for the start of the thread.
Why it is so important if it's old or new, as soon as from the begining of the thread until the end the subject and solutions are very good and linked together?

We can consider now a complete thread and anyone who is searching could have different solution but doesn't care if it was discussed during years or days, the solution is the key!

Best regards,
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Problem using sed to replace string in file umk Debian 12 02-01-2012 08:39 AM
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 01:34 PM
Using sed/awk to replace a string at a given position in anoopvraj Linux - Newbie 6 05-30-2009 07:59 AM
how to find and replace only the 2nd occurrence of similar string in a file hchoonbeng Linux - Newbie 1 10-08-2008 03:44 AM
SED replace string by occurrence uttam_h Programming 5 03-05-2008 10:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:24 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration