this is my file.
...
</script>
<!--START: Google Analytics --->
<script type="text/javascript"
src="../src/goog/ga_body.js"></script>
<!--END: Google Analytics --->
</body>
</html>
...
how do I delete every thing <!--START: Google Analytics --->
and <!--END: Google Analytics --->
inclusively? So effectively this:
<!--START: Google Analytics --->
<script type="text/javascript"
src="../src/goog/ga_body.js"></script>
<!--END: Google Analytics --->
will be gone. and this will be left i.e. that is nothing, the 4 lines will be replaced with nothing.
</script>
<nothing here 4 lines deleted>
</body>
</html>
I am looking at doing it in bash so maybe sed and awk might be my best bet, although python might be better.
EDIT1
This is something I have written before, but it is probably very poor coding, I will work off this find2PatternsAndDeleteTextInBetween.sh
:
#HEre I want to find 2 patterns and delete whats in between
#this example works
#this is the 2 patterns I want to fine Start and End
#have to use some escape characters here for this to show properly
# have to use \n for it to appear in this format
#<!-- Start of StatCounter Code for DoYourOwnSite -->
# text would go here
#<!-- End of StatCounter Code for DoYourOwnSite -->>
#b="<!-- Start of StatCounter Code for DoYourOwnSite -->"
#b2="<!-- End of StatCounter Code for DoYourOwnSite -->"
#p1="PATTERN-1"
#p2="PATTERN-2"
p1="<!-- Start of StatCounter Code for DoYourOwnSite -->"
p2="<!-- End of StatCounter Code for DoYourOwnSite -->"
fname="*.html"
num_of_files_pattern1=ls #grep $p1 fname
echo "fname(s) to apply the sed to:"
echo $fname
echo "num_of_files_pattern1 is:"
echo $num_of_files_pattern1
echo "Pattern1 is equal to:"
echo $p1
echo "Pattern2 is equal to:"
echo $p2
#this is current dir where the script is
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo "DIR is equal to:"
echo $DIR
#cd to the dir where I want to copy the files to:
cd "$DIR"
# this will find the pattern <\head> in all the .html files and place "This should appear before the closing head tag" this before it
# it will also make a backup with .bak extension
#sed -i.bak '/<\\head>/i\This should appear before the closing head tag' *.html
echo "sed on the file"
# this does the head part
#sed '/PATTERN-1/,/PATTERN-2/d' *.txt # this works
#sed "/$p1/,/$p2/d" *.txt # this works
#sed "/$p1/,/$p2/d" $fname # this works
sed -i.bak "/$p1/,/$p2/d" $fname # this works
EDIT2
This is what i ended up with, but there is a more robust answer below:
# ------------------------------------------------------------------
# [author] find2PatternsAndDeleteTextInBetween.sh
# Description
# Here I want to find 2 patterns and delete what's in between
# this example works
#
# EXAMPLE:
# this is the 2 patterns I want to find Start and End
# <!-- Start of StatCounter Code for DoYourOwnSite -->
# text would go here
# <!-- End of StatCounter Code for DoYourOwnSite -->>
#
# ------------------------------------------------------------------
p1="<!--START: Google Analytics --->"
p2="<!--END: Google Analytics --->"
fname=".html"
echo "fname(s) to apply the sed to:"
echo *"$fname"
echo -e "\n"
echo "Pattern1 is equal to:"
echo -e "$p1\n"
echo "Pattern2 is equal to:"
echo -e "$p2\n"
echo -e "PWD is: $PWD\n"
echo "sed on the file"
#sed '/PATTERN-1/,/PATTERN-2/d' *.txt # this works
#sed "/$p1/,/$p2/d" *.txt # this works
#sed "/$p1/,/$p2/d" $fname # this works
sed -i.bak "/$p1/,/$p2/d" *"$fname" # this works