Delete text or paragraph between two sections using sed

Posted on in Categories , , last updated August 9, 2017

I am a new Linux user. How do I use the sed command 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

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.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

12 comment

  1. but the command

    sed -e ‘/word1/,/word2/d’ deletes the whole line in case the given text is like this :

    unix word1 java word2

    if the text between word1 and word2 only is to be deleted then this is not the solution. Can I get some help on this aspect…. thnks in advance

  2. How to delete following line from a xml file.
    #vi package.xml
    drivers-3.6.1-${release}.${arch}.rpm
    ..
    ..
    #
    I tried sed -e ‘/drivers-3.6.1-${release}.${arch}.rpm /d’ /root/package.xml > /root/output.txt

  3. As for people asking Sam’s question (11.27.07), the problem is two fold. One, using sed’s pattern matching with the “d” (delete) clobbers whole lines, as you noted. The solution would seem to to use the “s” (substitute) form instead:
    sed 's/WORD1.*WORD2//g' input.txt
    BUT then you discover the larger problem: sed by default does all it’s processing one line at a time (e.g., it fills it’s processing buffer up to each ‘\n’ it encounters).
    The formal approaches include using sed’s “hold space” and “pattern space” commands, N, H/h,G/g,x as hinted here:
    http://www.gnu.org/software/sed/manual/html_node/Other-Commands.html
    I used that route in an email formatting script, but these days if i can get away with quick’n’dirty, i try to just strip all occurrences of ‘\n’ from the stream you give sed like this:
    cat input.txt | tr -d '\n' | sed 's/WORD1.*WORD2//g'
    No files i’ve worked on are big enough to blow sed’s buffer so far.

    1. What if there are several occurrences of WORD2, but you only want to delete what is before WORD1 and the first WORD2? It seems that it chooses the last occurrence of WORD2 instead, how do I change that?

    2. I’m sorry for asking this here, felt you could help me.

      How do I delete everything between (……), but leaving behind [..], in the below line:
      123(45[678]9)0

      Expected output: 123[678]0.

      Thanks in advance

  4. How to find and remove all the JavaScript code in a given HTML text? I mean, consider that in this comment, I’ve written something like an alert, how do you find it, and remove it?

  5. For the above code we can make it more simple by placing “-i” option in sed command,by this sed will remove the lines in the file *.html itself and..no need of output file(>$OUTF).

    #!/bin/bash
    # ALL HTML FILES
    FILES="*.html"
    # for loop read each file
    for f in $FILES
    do
    INF="$f"
    # replace javascript
    sed -i '/&lt;script type=&quot;text/javascript&quot;/,//d' $INF 
    done

Leave a Comment