first adventure with sed from bash

Essence_of_War

Platinum Member
Feb 21, 2013
2,650
4
81
Hey everyone,

I'm trying to write a bash script to replace a few strings in a text file of html, with some user defined stuff. I have a text file that has a marker, "XXX", where I'd like to pop in a number from a command line argument.

It seemed like 'sed' was the right tool for the job, so I tried something like this.

str1="XXX"
str2="$1"
reffile="ref_file.txt"
newfile="new_file.txt"
cp $reffile $newfile
sed "s/$str1/$str2/nwg $newfile"

with options:
s - substitution
n - suppress output to terminal
w - write them to file
g - do all matches

But when I actually run it, I get:
sed: -e expression #1, char 24: unknown option to 's'

And I don't really understand what that means, I thought that maybe there were somehow slashes in the string created by the "", and so I tried swapping my '/' in the sed command with '@', but that seems to give the same error.

The file ref_file.txt and new_file.txt both have lots of html tags, '<>', "", etc, could that be a problem?
 

Essence_of_War

Platinum Member
Feb 21, 2013
2,650
4
81
I think I fixed my problem. There were several:

1) I should have been using -i
2) The error was from the 'w' flag, it expected an additional file to write the changes to, but since I wanted to do them in-place, this was superfluous. I fixed it thusly:

sed -i "s/$str1/$str2/g" $newfile

and that seems to work fine, and do exactly what I was trying to do.