我即将编写一个 bash 脚本,该脚本应该删除找到搜索词的文本块(在纺织品中)。不幸的是我无法让它工作。
我有一个列表文件,看起来像这样
[valid_account]
user = doe.john
passw = 4567
mac = 11-22-33-44-bb-cc
s2 = qwe
[valid_account]
user = kent.clark
passw = 1234
mac = 00-11-22-33-aa-bb
s1 = abc
s2 = efg
[valid_account]
user = bond.james
passw = 6789
mac = 44-55-66-77-ee-ff
s4 = bnm
[valid_account]
user = parker.peter
passw = 9876
mac = 77-bb-cc-66-00-aa
s1 = abc
如果搜索词是00-11-22-33-aa-bb,我怎样才能删除结果是这样的搜索词的文本块?
[valid_account]
user = doe.john
passw = 4567
mac = 11-22-33-44-bb-cc
s2 = qwe
[valid_account]
user = bond.james
passw = 6789
mac = 44-55-66-77-ee-ff
s4 = bnm
[valid_account]
user = parker.peter
passw = 9876
mac = 77-bb-cc-66-00-aa
s1 = abc
答案1
使用 gnu sed
sed '/valid_account/!d;:A;N;$bB;/\n$/!bA;:B;/00-11-22-33-aa-bb/d' infile
答案2
awk -v delete_regex='00-11-22-33-aa-bb' -v RS=$'\n\n' -v ORS=$'\n\n' \
'$0 !~ delete_regex' inputfile
可以进行更改,以便搜索字符串不被解释为正则表达式。
答案3
和珀尔,紧凑/简洁的方式:
$ perl -00ne 'print if !/00-11-22-33-aa-bb/' file
或使用变量str
:
str='00-11-22-33-aa-bb' perl -00lne 'print if !/$ENV{"str"}/' file
输出 :
[valid_account]
user = doe.john
passw = 4567
mac = 11-22-33-44-bb-cc
s2 = qwe
[valid_account]
user = bond.james
passw = 6789
mac = 44-55-66-77-ee-ff
s4 = bnm
[valid_account]
user = parker.peter
passw = 9876
mac = 77-bb-cc-66-00-aa
s1 = abc