PDA

View Full Version : [SOLVED] Help with replacing multiple strings.



shymega
February 18th, 2012, 07:40 PM
Hi,

I'm trying to replace strings in a HTML Template and replace them with pre-defined variables.

I have tried sed but it just leaves the file as it was.

I am using BASH.

Thanks,
sm

r-senior
February 18th, 2012, 08:20 PM
I have tried sed but it just leaves the file as it was.
Sed is a stream editor - it edits a stream as it passes through. So you usually need something like:


sed -e 's/who/how/g' < infile.txt > outfile.txt

shymega
February 18th, 2012, 08:27 PM
I've just tried that - It still leaves the file as it was.

I'm aiming to replace a word (traffic, client and health) with a variable in my script. I've modified that sed command that you posted so it looks like this:


sed -e 's/traffic/'$TRAF'/g' < templatestatus.html > status.html


Thanks for the input.

--
sm

r-senior
February 18th, 2012, 08:46 PM
Could you demonstrate your problem with short, sample files and show the commands you are entering and the result you get?

shymega
February 18th, 2012, 08:49 PM
Here is the output of my sample commands:




Terminal> TRAF='111'
Terminal> touch status
Terminal> nano status
Terminal> sed -e 's/traffic/'$TRAF'/g' < templatestatus.html > status.html
Terminal> cat status
traffic
Terminal>

josephmills
February 18th, 2012, 08:52 PM
Here is the output of my sample commands:




Terminal> TRAF='111'
Terminal> touch status
Terminal> nano status
Terminal> sed -e 's/traffic/'$TRAF'/g' < templatestatus.html > status.html
Terminal> cat status
traffic
Terminal>



Terminal> TRAF="111"
Terminal> touch status
Terminal> nano status
Terminal> sed -e 's/traffic/$TRAF/g' templatestatus.html status.html
Terminal> cat status
traffic
Terminal>

or

#!usr/bin/env bash
clear
error="something went wrong"
TRAF="111";
touch status;
nano status;
if
sed -e 's|traffic|$TRAF|g' templatestatus.html status.html
then
echo "your file name has been changed"
else
echo "$error"
fi

shymega
February 18th, 2012, 08:56 PM
Thank you very much josephmills, it's working now.

I really appreciate it.

By the way, I +1'ed your thread for Ubuntu Membership.

--
sm