LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 10-17-2008, 05:44 AM   #1
raghupal
LQ Newbie
 
Registered: Mar 2006
Location: Hyderabad
Distribution: Fedora Core 8
Posts: 15

Rep: Reputation: 0
Smile 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
 
Old 10-17-2008, 06:26 AM   #2
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
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.
 
Old 10-17-2008, 07:22 AM   #3
raghupal
LQ Newbie
 
Registered: Mar 2006
Location: Hyderabad
Distribution: Fedora Core 8
Posts: 15

Original Poster
Rep: Reputation: 0
Smile script to change the number

Quote:
Originally Posted by bgeddy View Post
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
 
Old 10-17-2008, 07:31 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
 
Old 10-17-2008, 08:03 AM   #5
raghupal
LQ Newbie
 
Registered: Mar 2006
Location: Hyderabad
Distribution: Fedora Core 8
Posts: 15

Original Poster
Rep: Reputation: 0
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
 
Old 10-17-2008, 08:24 AM   #6
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
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
 
Old 10-17-2008, 08:28 AM   #7
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
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 !!
 
Old 10-17-2008, 08:29 AM   #8
raghupal
LQ Newbie
 
Registered: Mar 2006
Location: Hyderabad
Distribution: Fedora Core 8
Posts: 15

Original Poster
Rep: Reputation: 0
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
 
Old 10-17-2008, 08:36 AM   #9
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
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
 
Old 10-17-2008, 10:06 AM   #10
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
bgeddy is correct.

And *please* don't flag your topics as "urgent". OK?
 
Old 10-20-2008, 12:10 AM   #11
raghupal
LQ Newbie
 
Registered: Mar 2006
Location: Hyderabad
Distribution: Fedora Core 8
Posts: 15

Original Poster
Rep: Reputation: 0
thanks a lot to all.
I resolved my problem
 
Old 10-20-2008, 02:57 AM   #12
raghupal
LQ Newbie
 
Registered: Mar 2006
Location: Hyderabad
Distribution: Fedora Core 8
Posts: 15

Original Poster
Rep: Reputation: 0
Smile

Quote:
Originally Posted by bgeddy View Post
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
 
Old 10-20-2008, 07:26 AM   #13
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
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.
 
Old 10-20-2008, 06:19 PM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 10-21-2008, 12:49 AM   #15
raghupal
LQ Newbie
 
Registered: Mar 2006
Location: Hyderabad
Distribution: Fedora Core 8
Posts: 15

Original Poster
Rep: Reputation: 0
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.
 
  


Reply



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
shell script to find an word or words from a line rakesh.tandur Linux - General 5 05-13-2008 01:57 PM
[SOLVED] find a word in a file, and change a word beneath it ?? vikas027 Programming 10 02-14-2008 09:46 PM
shell script: find subwords with 'compilation' word suresheva31 Programming 11 10-05-2005 04:17 AM
How to find and change a specific text in a text file by using shell script Bassam Programming 1 07-18-2005 07:15 PM
How to read ans parse MS word file using a Linux Shell script. Alek Linux - General 2 11-10-2003 02:07 PM

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

All times are GMT -5. The time now is 01:35 AM.

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
Open Source Consulting | Domain Registration