Using Sed to Remove a Block of Text

Using sed to remove a block of text

$ cat text 
abc
<!-- BOF CLEAN -->
... a bunch of stuff
<!-- EOF CLEAN -->
def
$ sed '/<!-- BOF CLEAN -->/,/<!-- EOF CLEAN -->/d' text
abc
def

I cannot explain it any better than Sed One-Liners Explained, Part III: Selective Deletion of Certain Lines and Special Applications.

Remove block of text stored in one file from another file

Finally found the answer:

pcregrep -v -F -f <(seq 2 4) <(for J in {1..5};do seq 5;done)

for the large files you need to raise buffer.

How to remove a text block from a file with bash

You can use sed to remove the config for a given zone:

sed '/^zone "domain4.com" {$/,/^};/d' file

If you want a script that can take a zone as an argument, just add the she-bang and the argument:

#!/bin/bash
sed '/^zone "'"$1"'" {$/,/^};/d' file

bash remove block of text from file

Not awk, but this is straightforward with Perl 5, as @triplee pointed out. With the five-line input file you showed above as foo.txt:

perl -0777 -pe 's{^line 2\nline 3\n}{}gm' foo.txt

produces the desired three-line output.

Explanation:

  • -0777 causes perl to read the entire input as one string (see perlrun).
  • The /m modifier on the regex causes ^ to match at the beginning of a line (see perlre).
  • Edit ^ will also match at the beginning of the file, so you can detect blocks of lines even if there is not a newline before them.
  • The separators between the lines are literal \ns because $ matches before the \n with the /m modifier. Therefore, it's easier just to match the \n.

Thanks to this U&L SE answer by Stéphane Chazelas for the basics.

Using sed to delete all lines between two matching patterns

Use this sed command to achieve that:

sed '/^#/,/^\$/{/^#/!{/^\$/!d}}' file.txt

Mac users (to prevent extra characters at the end of d command error) need to add semicolons before the closing brackets

sed '/^#/,/^\$/{/^#/!{/^\$/!d;};}' file.txt

OUTPUT

# ID 1
$ description 1
blah blah
# ID 2
$ description 2
blah blah
blah blah

Explanation:

  • /^#/,/^\$/ will match all the text between lines starting with # to lines starting with $. ^ is used for start of line character. $ is a special character so needs to be escaped.
  • /^#/! means do following if start of line is not #
  • /^$/! means do following if start of line is not $
  • d means delete

So overall it is first matching all the lines from ^# to ^\$ then from those matched lines finding lines that don't match ^# and don't match ^\$ and deleting them using d.

Remove the contents of block based on condition

This might work for you (GNU sed):

sed '/{content-start}/{:a;N;/{content-end}/!ba;/labtest/d}' file

Gather up the lines between the start and end strings then test the collection for the required string and delete if positive.

Bash script iterate through file and delete block of text using sed

You can do it like below:-

 sed -i '/www.youtube.com/I,+6 d;$!N;/www.youtube.com/!P;D' certs-new.json

If your search string has been provided as command line parameter use like

sed -i '/'$1'/I,+6 d;$!N;/'$1'/!P;D' certs-new.json

How will it work, First it will search for pattern www.youtube.com and delete 6 lines below the pattern and second part of sed command will search for pattern www.youtube.com and delete the pattern line and one line above it.

In your example the output will be:-

[
{
"url" : "www.google.com",
"valid_from" : " Jul 31 10:16:13 2017 GMT",
"valid_till" : " Jul 31 10:16:13 2019 GMT",
"validity" : "Valid",
"days" : "464"
},
]

sed regexp find block of string in multiline block and remove string untill it end with space

You may try this gnu-sed that doesn't require a blank line after commented lines that start with /# AppServer1/:

sed -E '/^#[[:blank:]]*AppServer1:/I, /^#?[[:blank:]]*$|^($|[^#])/ s/#[[:blank:]]?//' file

some text here
some text here

AppServer1:
name: ${AppServer1.name}
ip: ${AppServer1.ip}

# AppServer2:
# name: ${AppServer1.name}
# ip: ${AppServer1.ip}

some text here
some text here

Details:

  • /^#[[:blank:]]*AppServer1:/I: Start range from a line that starts with # followed by 0 or more spaces and AppServer1: (ignore case)
  • ,/^#?[[:blank:]]*$|^($|[^#])/: Range end with a line that just has # OR a line that doesn't have # at line start
  • s/#[[:blank:]]?//: Removes first # followed by an optional space


Related Topics



Leave a reply



Submit