1

我无法替换文件中的文本,因为搜索短语包含单引号。

文件名名单.pinterp

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

我正在使用 bash 脚本。我想要改变的是:

path_to_output = '.'

至:

path_to_output = '/myWorkDir/ALL_NEW/post_processed_files'

我曾尝试使用 perl,但出现错误。

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.

我错过了什么?我还能用什么?

4

1 回答 1

2

实际上,您有两个问题:路径中的正斜杠将被 Perl 视为分隔正则表达式。将 perl 命令用双引号括起来(并使用不同的分隔符进行替换);做了:

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

#用来分隔正则表达式,并将外部单引号替换为双引号,其中单引号是可接受的(在大多数 Unix shell 中)。

于 2012-12-05T22:12:02.040 回答