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
\n
s 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 andAppServer1:
(ignore case),/^#?[[:blank:]]*$|^($|[^#])/
: Range end with a line that just has#
OR a line that doesn't have#
at line starts/#[[:blank:]]?//
: Removes first#
followed by an optional space
Related Topics
How to Record What Process or Kernel Activity Is Using the Disk in Gnu/Linux
Getting List of Network Devices Inside the Linux Kernel
Sending Keyboard Input to a Program from Command-Line
Help with Understanding a Very Basic Main() Disassembly in Gdb
Apt-Get Error: Sub-Process /Usr/Bin/Dpkg Returned an Error Code (1)
How to Determine If Code Is Running in Signal-Handler Context
How to Check Status of Urls from Text File Using Bash Shell Script
Upstart Calling Script (For Inserted Usb-Drive)
Difference Between Flat Memory Model and Protected Memory Model
Windows Authentication Headers Without .Net. Possible
How to Recursively Copy a Directory into Another and Replace Only the Files That Have Not Changed
Link a Static Library to a Shared One During Build
Define Alias That References Other Aliases
Using Linux How to Pass the Contents of a File as a Parameter to an Executable
How to Add Deploy Key for 2 Repo with 1 User on Github
How to Extract Numbers from a String
Error: Ld.So: Object Ld_Preload Cannot Be Preloaded: Ignored
How to Continue One Thread at a Time When Debugging a Multithreaded Program in Gdb