≡ Menu

sed regular expressions

Q. How do I find the text between the strings FOO and BAR inclusive using sed command line option?
[click to continue…]

Q. How do I use sed for selective deletion of certain lines? I have text as follows in file:
Line 1
Line 2
WORD1
Line3
Line 4
WORD2
Line5

I would like to delete all lines between WORD1 and WORD2 to produce final output:
Line 1
Line 2
Line5

A. For selective deletion of certain lines sed is the best tool. To print all of file EXCEPT section between WORD1 and WORD2 (2 regular expressions), use
$ sed '/WORD1/,/WORD2/d' input.txt > output.txt

Shell script to remove Javascript code

Here is my small script that reads all *.html files and removes javascript (script download link).

#!/bin/bash
# ALL HTML FILES
FILES="*.html"
# for loop read each file
for f in $FILES
do
INF="$f"
OUTF="$f.out.tmp"
# replace javascript
sed '/<script type="text\/javascript"/,/<\/script>/d' $INF > $OUTF
/bin/cp $OUTF $INF
/bin/rm -f $OUTF
done

Above shell script removes all occurrence of javascript.