Delete text between 2 delimiters with AWK

I have a file like this:

start of my file
Some lines
#start
other lines
blabla other lines
#end
end of my file

I would like to delete everything between #start and #end (#start and #end included) and export the result to a file.

Expected result :

start of my file
Some lines
end of my file

I know how to make a selection between delimiters

awk '/#start/,/#end/' 

but I can’t do the deletion.

>Solution :

The expression

awk '/#start/,/#end/'

is shorthand for

awk '/#start/,/#end/ { print $0 }'

If the implied default action is not the one you want, spell out what you do want.

awk '/#start/,/#end/ { next } 1'

says to print all lines (by way of another shorthand, 1, which selects the default action for all lines by virtue of having an address expression which is true for all lines) but skip that for lines in the region (next is the instruction to discard the current input line).

Leave a Reply