Leahcim
Leahcim

Reputation: 41919

unescaped newline inside substitute pattern

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

Answers (2)

glenn jackman
glenn jackman

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

fedorqui
fedorqui

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

Related Questions