LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bit complicated sed search & replace (https://www.linuxquestions.org/questions/programming-9/bit-complicated-sed-search-and-replace-544067/)

sharathkv25 04-07-2007 12:25 PM

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

druuna 04-07-2007 12:46 PM

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.

pixellany 04-07-2007 12:54 PM

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

sharathkv25 04-07-2007 01:11 PM

Thank you druuna & pixellany for reply.

drunna both of your solutions are not working :confused:

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

sharathkv25 04-07-2007 01:13 PM

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

sharathkv 04-07-2007 01:33 PM

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

druuna 04-07-2007 01:53 PM

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.


All times are GMT -5. The time now is 08:50 AM.