LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   URGENT----shell script to change word in file (https://www.linuxquestions.org/questions/programming-9/urgent-shell-script-to-change-word-in-file-677047/)

raghupal 10-17-2008 05:44 AM

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
Quote:

<VR NM="EID" VL="1603"/>
Please help me

Thanks in advance

bgeddy 10-17-2008 06:26 AM

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.

raghupal 10-17-2008 07:22 AM

script to change the number
 
Quote:

Originally Posted by bgeddy (Post 3313301)
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

colucix 10-17-2008 07:31 AM

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

raghupal 10-17-2008 08:03 AM

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

bgeddy 10-17-2008 08:24 AM

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

bgeddy 10-17-2008 08:28 AM

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 !!

raghupal 10-17-2008 08:29 AM

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

bgeddy 10-17-2008 08:36 AM

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

paulsm4 10-17-2008 10:06 AM

bgeddy is correct.

And *please* don't flag your topics as "urgent". OK?

raghupal 10-20-2008 12:10 AM

thanks a lot to all.
I resolved my problem

raghupal 10-20-2008 02:57 AM

Quote:

Originally Posted by bgeddy (Post 3313301)
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.

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.

Please help me

bgeddy 10-20-2008 07:26 AM

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.

chrism01 10-20-2008 06:19 PM

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.

raghupal 10-21-2008 12:49 AM

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 07:25 AM.