About Linux FAQ

Browse More FAQs:

Delete text or paragraph between two sections using sed

Posted by Vivek Gite [Last updated: April 12, 2008]

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.

Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

Related Other Helpful FAQs:

Discussion on This FAQ

  1. Sam Says:

    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. Johan Says:

    How can i make the script to look in subfolders?

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , ,

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.