如何用新的文本块替换文本块?
how to replace a block of text with new block of text?
如问题标题所述,我必须用新的文本块替换文件中的文本块
我到处搜索这个东西,但我找到的每一个解决方案都太具体了。难道不能创建一个 flexible/reusable 的函数吗?
具体来说,我需要一些选项,例如
1) File ( where changes are to be done )
2) Exiting block of text
3) New block of text
( 2nd & 3 option could be either as manually pasted text or cat $somefile)
因此我可以更改这 3 个并将脚本用于所有文本块替换情况,我相信它也会帮助很多其他人
举个例子,目前我需要用底部的文本替换下面的文本块,并说文件是 $HOME/block.txt。虽然我需要的解决方案很容易 reusable/flexible 如上所述
- name: Set default_volumes variable
set_fact:
default_volumes:
- "/opt/lidarr:/config"
- "/opt/scripts:/scripts"
- "/mnt:/mnt"
- "/mnt/unionfs/Media/Music:/music"
- name: Set default_volumes variable
set_fact:
default_volumes:
- "/opt/lidarr:/config"
- "/opt/scripts:/scripts"
- "/mnt:/mnt"
- "/mnt/unionfs/Media/Music:/music"
- "/mnt/unionfs/downloads/lidarr:/downloads-amd"
PS / 替换时我需要保留间距和缩进。
对于这样的任务,您可以只使用现有的命令而不是
重新发明轮子:
sed '/some text to change/,/with indentation/d; /a bit more/r new_file' your_file
我使用了两个示例文件:
# original file
some original text to keep
a bit more
some text to remove
- with indentation
rest of original text
is kept
和:
# replacement text
SOME TEXT TO ADD
- WITH DIFFERENT INDENTATION
- ANOTHER LEVEL
然后该命令首先删除两个之间的行并包括两个行
行匹配模式:
sed '/some text to change/,/with indentation/d;
然后使用模式从其他文件中读取替换文本
匹配旧文本开始的位置:
/a bit more/r new_file' your_file
要产生结果:
some original text to keep
a bit more
SOME TEXT TO ADD
- WITH DIFFERENT INDENTATION
- ANOTHER LEVEL
rest of original text
is kept
编辑
以上比我原来的方法好:
sed '/a bit more/q' your_file > composite; cat new_file >> composite; sed -n '/rest of original text/,/$/p' your_file >> composite
将 GNU awk 用于多字符 RS 和 ARGIND,这将适用于旧文本或新文本中的任何字符,包括正则表达式元字符、定界符、引号和反向引用,因为它只是进行文字字符串搜索和替换:
awk -v RS='^$' -v ORS= '
ARGIND==1 { old=[=10=]; next }
ARGIND==2 { new=[=10=]; next }
s=index([=10=],old) {
[=10=] = substr([=10=],1,s-1) new substr([=10=],s+length(old))
}
1' old new file
或者您可以在每个 Unix 机器上使用任何 shell 中的任何 awk 来做同样的事情:
awk -v ORS= '
{ rec = (FNR>1 ? rec RS : "") [=11=] }
FILENAME==ARGV[1] { old=rec; next }
FILENAME==ARGV[2] { new=rec; next }
END {
[=11=] = rec
if ( s=index([=11=],old) ) {
[=11=] = substr([=11=],1,s-1) new substr([=11=],s+length(old))
}
print
}
' old new file
例如:
$ head old new file
==> old <==
- name: Set default_volumes variable
set_fact:
default_volumes:
- "/opt/lidarr:/config"
- "/opt/scripts:/scripts"
- "/mnt:/mnt"
- "/mnt/unionfs/Media/Music:/music"
==> new <==
- name: Set default_volumes variable
set_fact:
default_volumes:
- "/opt/lidarr:/config"
- "/opt/scripts:/scripts"
- "/mnt:/mnt"
- "/mnt/unionfs/Media/Music:/music"
- "/mnt/unionfs/downloads/lidarr:/downloads-amd"
==> file <==
foo
- name: Set default_volumes variable
set_fact:
default_volumes:
- "/opt/lidarr:/config"
- "/opt/scripts:/scripts"
- "/mnt:/mnt"
- "/mnt/unionfs/Media/Music:/music"
bar
$ awk -v RS='^$' -v ORS= 'ARGIND==1{old=[=13=]; next} ARGIND==2{new=[=13=]; next} s=index([=13=],old){ [=13=]=substr([=13=],1,s-1) new substr([=13=],s+length(old))} 1' old new file
foo
- name: Set default_volumes variable
set_fact:
default_volumes:
- "/opt/lidarr:/config"
- "/opt/scripts:/scripts"
- "/mnt:/mnt"
- "/mnt/unionfs/Media/Music:/music"
- "/mnt/unionfs/downloads/lidarr:/downloads-amd"
bar
您的数据已使用 YAML 序列化。你应该这样对待它。
使用yq
yq eval '
.[0].set_fact.default_volumes +=
[ "/mnt/unionfs/downloads/lidarr:/downloads-amd" ]
'
yq
本身不支持就地编辑,但您可以使用 sponge
实现相同的功能。
yq eval '
.[0].set_fact.default_volumes +=
[ "/mnt/unionfs/downloads/lidarr:/downloads-amd" ]
' a.yaml | sponge a.yaml
使用 Perl
perl -MYAML -0777ne'
my $d = Load($_);
push @{ $d->[0]{set_fact}{default_volumes} },
"/mnt/unionfs/downloads/lidarr:/downloads-amd";
print Dump($d);
'
根据 ,就地编辑将如下所示:
perl -i -MYAML -0777ne'
my $d = Load($_);
push @{ $d->[0]{set_fact}{default_volumes} },
"/mnt/unionfs/downloads/lidarr:/downloads-amd";
print Dump($d);
' file.yaml
如问题标题所述,我必须用新的文本块替换文件中的文本块
我到处搜索这个东西,但我找到的每一个解决方案都太具体了。难道不能创建一个 flexible/reusable 的函数吗?
具体来说,我需要一些选项,例如
1) File ( where changes are to be done )
2) Exiting block of text
3) New block of text
( 2nd & 3 option could be either as manually pasted text or cat $somefile)
因此我可以更改这 3 个并将脚本用于所有文本块替换情况,我相信它也会帮助很多其他人
举个例子,目前我需要用底部的文本替换下面的文本块,并说文件是 $HOME/block.txt。虽然我需要的解决方案很容易 reusable/flexible 如上所述
- name: Set default_volumes variable
set_fact:
default_volumes:
- "/opt/lidarr:/config"
- "/opt/scripts:/scripts"
- "/mnt:/mnt"
- "/mnt/unionfs/Media/Music:/music"
- name: Set default_volumes variable
set_fact:
default_volumes:
- "/opt/lidarr:/config"
- "/opt/scripts:/scripts"
- "/mnt:/mnt"
- "/mnt/unionfs/Media/Music:/music"
- "/mnt/unionfs/downloads/lidarr:/downloads-amd"
PS / 替换时我需要保留间距和缩进。
对于这样的任务,您可以只使用现有的命令而不是 重新发明轮子:
sed '/some text to change/,/with indentation/d; /a bit more/r new_file' your_file
我使用了两个示例文件:
# original file
some original text to keep
a bit more
some text to remove
- with indentation
rest of original text
is kept
和:
# replacement text
SOME TEXT TO ADD
- WITH DIFFERENT INDENTATION
- ANOTHER LEVEL
然后该命令首先删除两个之间的行并包括两个行 行匹配模式:
sed '/some text to change/,/with indentation/d;
然后使用模式从其他文件中读取替换文本 匹配旧文本开始的位置:
/a bit more/r new_file' your_file
要产生结果:
some original text to keep
a bit more
SOME TEXT TO ADD
- WITH DIFFERENT INDENTATION
- ANOTHER LEVEL
rest of original text
is kept
编辑
以上比我原来的方法好:
sed '/a bit more/q' your_file > composite; cat new_file >> composite; sed -n '/rest of original text/,/$/p' your_file >> composite
将 GNU awk 用于多字符 RS 和 ARGIND,这将适用于旧文本或新文本中的任何字符,包括正则表达式元字符、定界符、引号和反向引用,因为它只是进行文字字符串搜索和替换:
awk -v RS='^$' -v ORS= '
ARGIND==1 { old=[=10=]; next }
ARGIND==2 { new=[=10=]; next }
s=index([=10=],old) {
[=10=] = substr([=10=],1,s-1) new substr([=10=],s+length(old))
}
1' old new file
或者您可以在每个 Unix 机器上使用任何 shell 中的任何 awk 来做同样的事情:
awk -v ORS= '
{ rec = (FNR>1 ? rec RS : "") [=11=] }
FILENAME==ARGV[1] { old=rec; next }
FILENAME==ARGV[2] { new=rec; next }
END {
[=11=] = rec
if ( s=index([=11=],old) ) {
[=11=] = substr([=11=],1,s-1) new substr([=11=],s+length(old))
}
print
}
' old new file
例如:
$ head old new file
==> old <==
- name: Set default_volumes variable
set_fact:
default_volumes:
- "/opt/lidarr:/config"
- "/opt/scripts:/scripts"
- "/mnt:/mnt"
- "/mnt/unionfs/Media/Music:/music"
==> new <==
- name: Set default_volumes variable
set_fact:
default_volumes:
- "/opt/lidarr:/config"
- "/opt/scripts:/scripts"
- "/mnt:/mnt"
- "/mnt/unionfs/Media/Music:/music"
- "/mnt/unionfs/downloads/lidarr:/downloads-amd"
==> file <==
foo
- name: Set default_volumes variable
set_fact:
default_volumes:
- "/opt/lidarr:/config"
- "/opt/scripts:/scripts"
- "/mnt:/mnt"
- "/mnt/unionfs/Media/Music:/music"
bar
$ awk -v RS='^$' -v ORS= 'ARGIND==1{old=[=13=]; next} ARGIND==2{new=[=13=]; next} s=index([=13=],old){ [=13=]=substr([=13=],1,s-1) new substr([=13=],s+length(old))} 1' old new file
foo
- name: Set default_volumes variable
set_fact:
default_volumes:
- "/opt/lidarr:/config"
- "/opt/scripts:/scripts"
- "/mnt:/mnt"
- "/mnt/unionfs/Media/Music:/music"
- "/mnt/unionfs/downloads/lidarr:/downloads-amd"
bar
您的数据已使用 YAML 序列化。你应该这样对待它。
使用yq
yq eval '
.[0].set_fact.default_volumes +=
[ "/mnt/unionfs/downloads/lidarr:/downloads-amd" ]
'
yq
本身不支持就地编辑,但您可以使用 sponge
实现相同的功能。
yq eval '
.[0].set_fact.default_volumes +=
[ "/mnt/unionfs/downloads/lidarr:/downloads-amd" ]
' a.yaml | sponge a.yaml
使用 Perl
perl -MYAML -0777ne'
my $d = Load($_);
push @{ $d->[0]{set_fact}{default_volumes} },
"/mnt/unionfs/downloads/lidarr:/downloads-amd";
print Dump($d);
'
根据
perl -i -MYAML -0777ne'
my $d = Load($_);
push @{ $d->[0]{set_fact}{default_volumes} },
"/mnt/unionfs/downloads/lidarr:/downloads-amd";
print Dump($d);
' file.yaml