Question

I need a way to read and delete text between two different strings found in some file, then delete the two strings. Like a "cut command." I would like to have the text stored in a variable.

I saw the post about reading text between two strings, but I could not figure out how to delete it as well.

I intend to execute the stored text in bash. Efficiency is desirable. This script is not going to be used on large files, but it may be executed many times sequentially so the faster the script works the better.

The stored text will usually have special characters.

Thanks

Was it helpful?

Solution

Specify the beginning and ending strings via the environment, and the file to use on the perl command line:

export START_STRING='abc def'
export END_STRING='ghi jkl'
perl -0777 -i -wpe's/\Q$ENV{START_STRING}\E(.*)\Q$ENV{END_STRING}\E/s;print STDERR $1' file_to_use 2>savedtext
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top