如何解决如何用新的文本块替换文本块?
正如问题标题所指定的,我必须用新的文本块替换文件中的文本块
我已经到处搜索这件事,但我找到的每个解决方案都对问题太具体了。难道不能创建一个灵活/可重用的函数吗?
具体来说,我需要一些具有类似选项的东西
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 。虽然我需要如上所述的易于重用/灵活的解决方案
- 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 / 替换时我需要保留间距和缩进。
解决方法
您的数据使用 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);
'
根据 specifying file to process to Perl one-liner,就地编辑如下所示:
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
,
将 GNU awk 用于多字符 RS 和 ARGIND,这将适用于旧文本或新文本中的任何字符,包括正则表达式元字符、分隔符、引号和反向引用,因为它只是进行文字字符串搜索和替换:
Function<ClientResponse,Mono<ResponseWrapper<T>>
或者你可以在每个 Unix 机器上的任何 shell 中使用任何 awk 来做同样的事情:
awk -v RS='^$' -v ORS= '
ARGIND==1 { old=$0; next }
ARGIND==2 { new=$0; next }
s=index($0,old) {
$0 = substr($0,1,s-1) new substr($0,s+length(old))
}
1' old new file
例如:
awk -v ORS= '
{ rec = (FNR>1 ? rec RS : "") $0 }
FILENAME==ARGV[1] { old=rec; next }
FILENAME==ARGV[2] { new=rec; next }
END {
$0 = rec
if ( s=index($0,old) ) {
$0 = substr($0,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
,
对于这样的任务,你可以只使用现有的命令而不是 重新发明轮子:
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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。