LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-07-2007, 12:25 PM   #1
sharathkv25
Member
 
Registered: Jul 2006
Distribution: HP-UX
Posts: 46

Rep: Reputation: 15
Bit complicated sed search & replace


Hi,

I have an html file as:

Code:
....
....
....
<PARAM NAME="separateFrame"      VALUE="true" >
<PARAM NAME="serverApp"          VALUE="default" >
<PARAM NAME="serverArgs"         VALUE="module=/xxx/xxxx/xxxxx/xxxxx.xxx useSDI=yes" >
<PARAM NAME="serverPort"         VALUE="9000" >
<PARAM NAME="splashScreen"       VALUE="/xxxx/xxxxx.gif" >
<PARAM NAME="title"              VALUE="xxxxxx" >
....
....
....
....
....
serverPort         ="9000"
splashScreen       ="/xxxxx/xxxxx.gif"
title              ="xxxxxxx"
webformsTitle      ="xxxxxx"
>
<NOEMBED>
</COMMENT>
</NOEMBED></EMBED>
</OBJECT></CENTER>
</BODY>
</HTML>
Now I need to replace the value of "serverPort" parameter. i,e replace 9000 with my own port value.

All the other contents of the HTML file will be the same except for the value of "serverPort".

I am not an expert in sed, so any help is appreciated.
Thank you
 
Old 04-07-2007, 12:46 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Hi,

This is one way of doing just that:

sed 's/\(serverPort" VALUE="\)9000"/\1NEWVALUE"/' infile

The part between \( and \) can be represented as \1 (backreferencing) in the replacement part (makes the sed command a bit shorter. Here's the same one without backreferencing:

sed 's/serverPort" VALUE="9000"/serverPort" VALUE="NEWVALUE"/' infile

Hope this helps.
 
Old 04-07-2007, 12:54 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
If the string "####" is unique, then just search on that and replace with your desired number, eg:

serv = filename
newserv = new filename

cat serv|sed -r 's/"[0-9]{4}"/"1234"/' >newserv

This matches any string of four numbers in double-quotes

Here is my favorite sed tutorial:
http://www.grymoire.com/Unix/Sed.html#uh-8
 
Old 04-07-2007, 01:11 PM   #4
sharathkv25
Member
 
Registered: Jul 2006
Distribution: HP-UX
Posts: 46

Original Poster
Rep: Reputation: 15
Thank you druuna & pixellany for reply.

drunna both of your solutions are not working

I ended up with my own crude hack here. However it's not replacing the 1st serverPort value.

this is the crude simple hack I came up with:

Code:
# Read SERVER_PORT value from file
server_port_val=`grep -i SERVER_PORT /etc/conf.env`

# Get value of paramter SERVER_PORT
server_port_val=`echo $server_port_val | cut -d "=" -f 2`
server_port_val=`echo $server_port_val | cut -d " " -f 1`

# Apply the new Server_Port Value to HTML file

sed -e 's|="9000"|="'$server_port_val'"|' static_jinit.html > 1.html
mv 1.html static_jinit.html
sed -e 's|="9000"|="'$server_port_val'"|' static_jinit.html > 1.html
Now, when I execute this script this is what I get in "1.html"

Code:
...
...
<PARAM NAME="serverArgs"         VALUE="module=/xxx/xxxx/xxxx/xxxxx useSDI=yes" >
<PARAM NAME="serverPort"         VALUE="7899" >
<PARAM NAME="splashScreen"       VALUE="/forms/xxxxx" >
...
...
...
serverArgs         ="module=/xxx/xxx/xxx/xxxxx useSDI=yes"
serverPort         ="7777"
splashScreen       ="/xxxx/xxxxx"
As you can see the 2nd serverPort is changed correctly to 7777. Not the first one???

Thanks
 
Old 04-07-2007, 01:13 PM   #5
sharathkv25
Member
 
Registered: Jul 2006
Distribution: HP-UX
Posts: 46

Original Poster
Rep: Reputation: 15
sorry the actual code is

Code:
# Read SERVER_PORT value from file
server_port_val=`grep -i SERVER_PORT /etc/conf.env`

# Get value of paramter SERVER_PORT
server_port_val=`echo $server_port_val | cut -d "=" -f 2`
server_port_val=`echo $server_port_val | cut -d " " -f 1`

# Apply the new Server_Port Value to HTML file

sed -e 's|VALUE="9000"|VALUE="'$server_port_val'"|' static_jinit.html > 1.html
mv 1.html static_jinit.html
sed -e 's|="9000"|="'$server_port_val'"|' static_jinit.html > 1.html
Had missed VALUE in first sed.

Thanks
 
Old 04-07-2007, 01:33 PM   #6
sharathkv
Member
 
Registered: Jul 2003
Distribution: HP-UX
Posts: 35

Rep: Reputation: 15
thank you all for replies.

I will use this crude but working hack I came up with.

Code:
# Read SERVER_PORT value from file
server_port_val=`grep -i SERVER_PORT /etc/conf.env`
server_port_val=`echo $server_port_val | cut -d "=" -f 2`
server_port_val=`echo $server_port_val | cut -d " " -f 1`

# Apply the new Server_Port Value to HTML file

sed -e 's|VALUE="9000"|VALUE="'$server_port_val'"|' static_jinit.html > 1.html
sed -e 's|="9000"|="'$server_port_val'"|' 1.html > 2.html
rm 1.html
mv 2.html static_jinit.html
Thanks
 
Old 04-07-2007, 01:53 PM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Hi,

The amount of spaces in the string are also important:
Code:
sed 's/serverPort"         VALUE="[0-9]*"/serverPort"         VALUE="NEWVALUE"/' infile
Or:

sed 's/\(serverPort"[ ]*VALUE="\)[0-9]*"/serverPort" VALUE="NEWVALUE"/' infile

the [ ]* part should hold a space and a tab character.
 
  


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
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 12:13 AM
sed search & replace sharathkv25 Programming 2 03-07-2007 10:16 AM
Perl Multi-Line Search & Replace... Can I?... donv2 Linux - Newbie 2 01-30-2007 09:59 PM
search & replace raj_sony2001 Linux - General 4 10-05-2006 02:05 PM
Best search & replace program geokker Linux - Software 3 09-28-2005 03:41 PM

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

All times are GMT -5. The time now is 12:10 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