在命令行中查找并替换多行文本 [英] Find and replace multiple lines of text from command line
问题描述
我正在尝试使用还执行其他操作的脚本来编辑配置文件.
I am trying to edit a config file using a script that also performs other operations.
脚本需要查找某些连续的文本行并将其删除. 它需要粘贴新配置,该新配置从与旧配置所在的行相同的行开始.在新旧配置中,空间的使用都很重要.
The script needs to find certain consecutive lines of text and remove these. In place it needs to paste a new configuration starting on the same line as where the old configuration was. In both the old and the new configuration, use of spaces matters.
在configfile.php中,此文本块:
in configfile.php, this block of text:
'session' =>
array (
'save' => 'files',
),
需要找到并替换为我在同一位置放在replace.txt中的内容.
needs to be found and replaced with content I put in replacement.txt at the same location.
基于对类似问题的回答,我尝试过的事情之一是:
Based on answers to similar questions, one of the things I tried is this:
cat replacement.txt <(sed -i '1,/ 'session' => \n array (\n 'save' => 'files',\n ),/d' configfile.php) > configfile.php
这将导致一个空文件.
cat replacement.txt <(sed -i '1,/ 'session' => \n array (\n 'save' => 'files',\n ),/d' configfile.php) > newfile.php
这和cat replace.txt> newfile.php一样,我想编辑一个文件,而不是创建一个新文件.
This does the same as cat replacement.txt > newfile.php does and I want to edit a file, not create a new file.
有人知道我的语法有什么问题以及如何解决吗?
Does anybody know what is wrong with my syntax and how I can fix it?
I've asked this question before here, but I didn't completely understand myself what I wanted, so it got a bit too elaborate and confusing. I use Debian Linux.
编辑基于评论:
replacement.txt看起来像这样:
The replacement.txt looks like this:
'session' =>
array (
'save' => 'redis',
'redis' =>
array (
'host' => '127.0.0.1',
'other_variable' => 'other_setting',
)
)
),
已经按照应该的方式缩进了,可以按原样插入. 使用awk或sed都可以.
It is already indented the way it should be and can be inserted as is. Use of awk or sed is both fine.
推荐答案
使用GNU awk进行多字符RS:
With GNU awk for multi-char RS:
$ cat tst.awk
BEGIN { RS="^$"; ORS="" }
ARGIND==1 { old = $0 }
ARGIND==2 { new = $0 }
ARGIND==3 {
if ( start = index($0,old) ) {
$0 = substr($0,1,start-1) new substr($0,start+length(old))
}
print
}
$ awk -f tst.awk target.txt replacement.txt configfile.php
foo
'session' =>
array (
'save' => 'redis',
'redis' =>
array (
'host' => '127.0.0.1',
'other_variable' => 'other_setting',
)
)
),
bar
以上内容在这些输入文件上运行:
The above was run on these input files:
$ cat target.txt
'session' =>
array (
'save' => 'files',
),
$
$ cat replacement.txt
'session' =>
array (
'save' => 'redis',
'redis' =>
array (
'host' => '127.0.0.1',
'other_variable' => 'other_setting',
)
)
),
$
$ cat configfile.php
foo
'session' =>
array (
'save' => 'files',
),
bar
$
其他问题可能是:
$ cat tst.awk
FNR==1 { ++argind }
argind==1 { old = (FNR>1 ? old RS : "") $0 }
argind==2 { new = (FNR>1 ? new RS : "") $0 }
argind==3 { rec = (FNR>1 ? rec RS : "") $0 }
END {
if ( start = index(rec,old) ) {
rec = substr(rec,1,start-1) new substr(rec,start+length(old))
}
print rec
}
$ awk -f tst.awk target.txt replacement.txt configfile.php
foo
'session' =>
array (
'save' => 'redis',
'redis' =>
array (
'host' => '127.0.0.1',
'other_variable' => 'other_setting',
)
)
),
bar
这篇关于在命令行中查找并替换多行文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!