Bit complicated sed search & replace
Hi,
I have an html file as: Code:
.... 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 |
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. |
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 |
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 Code:
... Thanks |
sorry the actual code is
Code:
# Read SERVER_PORT value from file Thanks |
thank you all for replies.
I will use this crude but working hack I came up with. Code:
# Read SERVER_PORT value from file |
Hi,
The amount of spaces in the string are also important: Code:
sed 's/serverPort" VALUE="[0-9]*"/serverPort" VALUE="NEWVALUE"/' infile 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. |