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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
10-17-2008, 05:44 AM
|
#1
|
LQ Newbie
Registered: Mar 2006
Location: Hyderabad
Distribution: Fedora Core 8
Posts: 15
Rep:
|
URGENT----shell script to change word in file
I want to replace a word in a particular file. I am attaching file here
Quote:
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- edited with XML Spy v3.5 NT (http://www.xmlspy.com) by Umashankar (NDS) -->
<DF NM="REN_EPG_EVENT_TABLE">
<DB NM="EVENTS_DB">
<VT NM="PROG_EVENT">
<IT NM="COMBO1_3 - Event 140">
<VT NM="OPI_OFFERS">
<IT NM="OPI_OFFERS_1">
<VR NM="OPI" VL="13073"/>
<VR NM="SUBSCRIBING_OFFERS" VL="7"/>
</IT>
</VT>
<GP NM="HOME_PAGE">
<VR NM="PICTURE" VL="dtv-d://epg_welcome.pictures:epg-050428-teva.jpg"/>
<VR NM="BASELINE" VL="Acceuil baseline"/>
</GP>
<GP NM="EVENT_PAGE">
<VR NM="PICTURE" VL="dtv-d://epg_welcome.pictures:epg-050429-pinktv.jpg"/>
<VR NM="TITLE" VL="title 1"/>
<GP NM="TEXT_COLOR">
<VR NM="R" VL="12"/>
<VR NM="G" VL="23"/>
<VR NM="B" VL="55"/>
</GP>
<GP NM="BG_COLOR">
<VR NM="R" VL="100"/>
<VR NM="G" VL="100"/>
<VR NM="B" VL="100"/>
</GP>
<VR NM="SHORT_SUMMARY" VL="resume court 1"/>
<VR NM="LONG_SUMMARY" VL="resume long 1"/>
</GP>
<VR NM="ONID" VL="192"/>
<VR NM="TSID" VL="1540"/>
<VR NM="SID" VL="2004"/>
<VR NM="EID" VL="1603"/>
<VT NM="IMAGE">
<IT NM="PARIS1ERA">
<VR NM="PICTURE" VL="dtv-d://epg_welcome.pictures:epg-060211-paris1era.jpg"/>
</IT>
</VT>
</IT>
</VT>
<VT NM="SERVICE_EVENT">
<IT NM="2004">
<VT NM="OPI_OFFERS">
<IT NM="OPI_OFFERS_1">
<VR NM="OPI" VL="13073"/>
<VR NM="SUBSCRIBING_OFFERS" VL="2259289122816"/>
</IT>
</VT>
<GP NM="HOME_PAGE">
<VR NM="PICTURE" VL="dtv-d://epg_welcome.pictures:epg-060214-cineemotion.jpg"/>
<VR NM="BASELINE" VL="baseline 2"/>
</GP>
<GP NM="EVENT_PAGE">
<VR NM="PICTURE" VL="dtv-d://epg_welcome.pictures:epg-050429-pinktv.jpg"/>
<VR NM="TITLE" VL="title 2"/>
<GP NM="TEXT_COLOR">
<VR NM="R" VL="60"/>
<VR NM="G" VL="60"/>
<VR NM="B" VL="60"/>
</GP>
<GP NM="BG_COLOR">
<VR NM="R" VL="200"/>
<VR NM="G" VL="200"/>
<VR NM="B" VL="200"/>
</GP>
<VR NM="SHORT_SUMMARY" VL="resume court 2"/>
<VR NM="LONG_SUMMARY" VL="resume long 2"/>
</GP>
<VR NM="SERVICE_TYPE" VL="1"/>
<VR NM="ORIGINAL_NETWORK_ID" VL="192"/>
<VR NM="TRANSPORT_STREAM_ID" VL="1540"/>
<VR NM="SERVICE_ID" VL="2004"/>
</IT>
</VT>
</DB>
</DF>
|
In the above file i want to change the number(1603 is not fixed and my not be fixed digits) to some other number in this line
Please help me
Thanks in advance
|
|
|
10-17-2008, 06:26 AM
|
#2
|
Senior Member
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810
|
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.
|
|
|
10-17-2008, 07:22 AM
|
#3
|
LQ Newbie
Registered: Mar 2006
Location: Hyderabad
Distribution: Fedora Core 8
Posts: 15
Original Poster
Rep:
|
script to change the number
Quote:
Originally Posted by bgeddy
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.
|
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
|
but it is giving wrong output like this
Quote:
<VR NM="EID" VL="1883 ^M"/>
|
please help me
|
|
|
10-17-2008, 07:31 AM
|
#4
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
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.
Code:
dos2unix epg_ren_event_id
|
|
|
10-17-2008, 08:03 AM
|
#5
|
LQ Newbie
Registered: Mar 2006
Location: Hyderabad
Distribution: Fedora Core 8
Posts: 15
Original Poster
Rep:
|
i want to remove all spaces from file content. i tried below one
Quote:
sed -i 's/\ *//' event_info.log
|
but it is not working properly
can anyone help me
|
|
|
10-17-2008, 08:24 AM
|
#6
|
Senior Member
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810
|
I agree it looks like a Dos file - if you have the fromdos utility andf want to do it in one you may do this :
Code:
sed -i "s/<VR NM=\"EID\" VL=\"[0-9]*\"\/>/<VR NM=\"EID\" VL=\"$(cat epg_ren_event_id | fromdos)\"\/>/" file.xml
|
|
|
10-17-2008, 08:28 AM
|
#7
|
Senior Member
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810
|
Quote:
i want to remove all spaces from file content. i tried below one
|
Try the global switch like this :
Code:
sed -i "s/ //g" event_info.log
I think you should brush up on your sed !!
|
|
|
10-17-2008, 08:29 AM
|
#8
|
LQ Newbie
Registered: Mar 2006
Location: Hyderabad
Distribution: Fedora Core 8
Posts: 15
Original Poster
Rep:
|
thanks for reply.
i am having spaces after the number in my epg_ren_event_id file.
I want to remove those spaces. then your script will work perfectly.
i need a script to remove all spaces and tabs in a file content.
please help me
|
|
|
10-17-2008, 08:36 AM
|
#9
|
Senior Member
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810
|
Quote:
i need a script to remove all spaces and tabs in a file content.
|
OK - its probably easier to use translate like this :
Code:
cat epg_ren_event_id | tr -d ["\t "] > fixedfile.txt
Then run the sed command on fixedfile.txt
|
|
|
10-17-2008, 10:06 AM
|
#10
|
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Rep: 
|
bgeddy is correct.
And *please* don't flag your topics as "urgent". OK?
|
|
|
10-20-2008, 12:10 AM
|
#11
|
LQ Newbie
Registered: Mar 2006
Location: Hyderabad
Distribution: Fedora Core 8
Posts: 15
Original Poster
Rep:
|
thanks a lot to all.
I resolved my problem
|
|
|
10-20-2008, 02:57 AM
|
#12
|
LQ Newbie
Registered: Mar 2006
Location: Hyderabad
Distribution: Fedora Core 8
Posts: 15
Original Poster
Rep:
|
Quote:
Originally Posted by bgeddy
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.
|
I succeed with your script
Now i am having so many lines like below with different values.
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.
Please help me
|
|
|
10-20-2008, 07:26 AM
|
#13
|
Senior Member
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810
|
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.
|
|
|
10-20-2008, 06:19 PM
|
#14
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443
|
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.
|
|
|
10-21-2008, 12:49 AM
|
#15
|
LQ Newbie
Registered: Mar 2006
Location: Hyderabad
Distribution: Fedora Core 8
Posts: 15
Original Poster
Rep:
|
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.
|
|
|
All times are GMT -5. The time now is 01:26 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|