For Linux and Shell scripting.

Remove everything in between two words from a file

Leave a comment

You can use the script below if you want to remove lines/words in between two specific words. Run it like: “./scriptname filename” where filename is name of the file from which you want to remove.

You will have to edit following fields in the script:
“Remove from”
“Remove upto here”

The script will match first occurence of “Remove from” and will remove upto the first occurence of “Remove upto here”. But, it won’t stop there…….. it will search for the next occurance of “Remove from” and will delete everything upto next “Remove upto here”. The contents will be removed only if it matches both “Remove from” and “Remove upto here”

#! /bin/bash

function remove_it () {
sed -i ‘/Remove from/{
/Remove upto here/d
b loop
}’ $1

remove_it $1

Note: This script will rewrite the existing file. If you don’t want to rewrite the existing file, remove “-i” option from “sed”. And then redirect the output to a new file


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s