I need to check if a file contains a specific two strings and if it does, remove all the text between them (strings included). Let's say I have a file text
which looks something like this:
line of text
line of text
line of text
line of text
#1st string
line of text
line of text
line of text
#2nd string
Now I would like to delete all the lines between #1st string
and #2nd string
and those two strings included.
I tried getting the whole text into a variable and then removing them like that:
prefix="#1st string"
suffix="#2nd string"
#content of a file into variable
tempfile=$( cat text )
tempfile=${tempfile#"$prefix"}
tempfile=${tempfile%"$suffix"}
echo ${tempfile}
Unfortunately, this doesn't work because the text file contains some commands which somehow list the content of the current directory and blend the output into the variable which leaves the file corrupted. Can I prevent that somehow, or is there a better way of achieving this altogether?