Reputation: 41919
I have a txt file with a list of 100 countries, without quotation marks around them. I am trying to change this
Canada
USA
into this
countries['Canada']=true
etc.
This is the sed command I am trying, with '\1' representing the country in quotation marks.
sed -e "s/\(.*\)/countries['\1']=true" source.txt > output.txt
The error I'm getting is
unescaped newline inside substitute pattern
What sed command do I need to achieve what I'm trying to do, and why am I getting this error
Upvotes: 1
Views: 5165
Reputation: 246807
I would just add stuff at the beginning and end:
sed -e "s/^/countries['/" -e "s/$/']=true/" source.txt > output.txt
Upvotes: 2
Reputation: 289645
You just missed a trailing /
at the end:
v
$ sed -e "s/\(.*\)/countries['\1']=true/" file
countries['Canada']=true
countries['USA']=true
Note also that you don't need to catch group, just match everything with .*
and then use &
to print it back:
|-------------|
vv v
$ sed -e "s/.*/countries['&']=true/" a
countries['Canada']=true
countries['USA']=true
Upvotes: 3