Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading