Erik Noble
Erik Noble

Reputation: 11

having trouble replacing text in file b/c phrase contains single quotes

I'm having trouble replacing text in a file because the search phrase contains single quotes.

FILENAME namelist.pinterp

&io
    path_to_input  = '.'
    input_name     = 'wrfout_d01_2006-09*00'
    path_to_output = '.'
  /

I'm using a bash script. All I want to change is:

path_to_output = '.'

to:

path_to_output = '/myWorkDir/ALL_NEW/post_processed_files'

I have tried using perl but I get errors.

perl -pi -e 's/path_to_output = '.'/ path_to_output =   '/myWorkDir/ALL_NEW/post_processed_files'g;' namelist.pinterp

ERROR
./myPinterp.bash: line 13: path_to_output = '.': command not found
Bareword found where operator expected at -e line 1, near "s/path_to_output = ./ path_to_output = /myWorkDir"
syntax error at -e line 1, near "s/path_to_output = ./ path_to_output = /myWorkDir"
Execution of -e aborted due to compilation errors.

What am I missing? What else can I use?

Upvotes: 1

Views: 83

Answers (1)

dbrobins
dbrobins

Reputation: 499

In fact you have two issues: the forward slashes in the path will be seen by Perl as delimiting the regular expression. Wrap the perl commands in double quotes (and use different delimiters for the replacement); make it:

perl -pi -e "s#path_to_output = '.'#path_to_output = '/myWorkDir/ALL_NEW/post_processed_files#g;" namelist.pinterp

I am using # to delimit the regular expression, and replaced the outer single quotes with double quotes, inside which single quotes are acceptable (in most Unix shells).

Upvotes: 2

Related Questions