ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810
Rep:
Code:
sed -i "s/<VR NM=\"EID\" VL=\"[0-9]*\"\/>/<VR NM=\"EID\" VL=\"2222\"\/>/" file.xml
This will replace any number found in the line <VR NM="EID" VL="1603"/> - (in this case it is 1603 but it could be any number) with 2222. Replace 2222 with the number you choose to replace. I have named the file "file.xml" but you should adjust to suit your file name.
sed -i "s/<VR NM=\"EID\" VL=\"[0-9]*\"\/>/<VR NM=\"EID\" VL=\"2222\"\/>/" file.xml
This will replace any number found in the line <VR NM="EID" VL="1603"/> - (in this case it is 1603 but it could be any number) with 2222. Replace 2222 with the number you choose to replace. I have named the file "file.xml" but you should adjust to suit your file name.
thanks a lot. I am getting.
Now i want to replace that number from cat command like
cat id ==== displays 1883 number
i tried like this
Quote:
sed -i "s/<VR NM=\"EID\" VL=\"[0-9]*\"\/>/<VR NM=\"EID\" VL=\"`cat epg_ren_event_id`\"\/>/" file.xml
The file epg_ren_event_id looks like a file created in DOS, hence the control character ^M which stats for the sequence \r\n (carriage return + newline). You have to convert it in unix format. E.g.
sed -i "s/<VR NM=\"EID\" VL=\"[0-9]*\"\/>/<VR NM=\"EID\" VL=\"2222\"\/>/" file.xml
This will replace any number found in the line <VR NM="EID" VL="1603"/> - (in this case it is 1603 but it could be any number) with 2222. Replace 2222 with the number you choose to replace. I have named the file "file.xml" but you should adjust to suit your file name.
I succeed with your script
Now i am having so many lines like below with different values.
Quote:
<VR NM="EID" VL="1603"/>
Now i want to change the number with different numbers across the file.
I mean on first occurrence i want to replace with 4444 and at second occurrence i want to replace with 5555 and so on.
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810
Rep:
Well you certainly keep changing the goalposts on this one ! Have you tried anything to solve this ? I recommended you look into sed - this now looks more like an awk problem. There is an excellent tutorial book on both here.
On a broader note - there may be better ways of parsing XML files - python has excellent support for XML in its libraries. It may be worth looking into if you really want to get clever with this.
Oh - and it goes without saying - I hope this isn't homework... You should experiment with solutions yourself to learn.
I agree you should try a proper XML parser. Perl has several modules for that. Sounds like you want a general solution, so sed/awk are probably not the way to go.
thanks for reply.
XML files are fixed. i want to change the numbers automatically. that is my final goal.
i am new to scripting. thats why i am going step by step.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.