0

This question already has an answer here:

I am about to write a bash script which should delete a text block (in a textile), where the search word was found. Unfortunately i can not get it to work.

I have a list-file which looks like that

[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

if the search word is 00-11-22-33-aa-bb, how can i delete the text block with the search word that the result is this?

[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

marked as duplicate by αғsнιη, Timothy Martin, G-Man, Archemar, Kiwy Apr 5 '18 at 8:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Where does this file is coming from ? Seems like a config of an application. Which one ? – Gilles Quenot Apr 4 '18 at 18:03
1

With gnu sed

sed '/valid_account/!d;:A;N;$bB;/\n$/!bA;:B;/00-11-22-33-aa-bb/d' infile
1
awk -v delete_regex='00-11-22-33-aa-bb' -v RS=$'\n\n' -v ORS=$'\n\n' \
    '$0 !~ delete_regex' inputfile

Could be changed so that the search string is not interpreted as regex.

0

With , compact/concise way :

$ perl -00ne 'print if !/00-11-22-33-aa-bb/' file

or using a variable str :

str='00-11-22-33-aa-bb' perl -00lne 'print if !/$ENV{"str"}/' file

Output :

[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

Not the answer you're looking for? Browse other questions tagged or ask your own question.