Delete text or paragraph between two sections using sed
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:
- Howto delete empty lines using sed command under Linux / UNIX
- Howto: Linux command line utilities for removing blank lines from text files
- Linux / UNIX Display Lines Common in Two Files
- Qmail delivering mail ~/Mailbox home directories
- UNIX / Linux: vi / vim perform search and replace operation
Discussion on This FAQ
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!
Tags: paragraph, regular expressions, sed command, sed delete lines between pattern, sed delete lines between words, sed regular expressions



November 27th, 2007 at 9:44 am
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
January 8th, 2008 at 10:22 am
How can i make the script to look in subfolders?