Delete text or paragraph between two sections using sed

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
Line 4

I would like to delete all lines between WORD1 and WORD2 to produce final output:

Line 1
Line 2

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).

# for loop read each file
for f in $FILES
# replace javascript
sed '/<script type="text\/javascript"/,/<\/script>/d' $INF > $OUTF
/bin/cp $OUTF $INF
/bin/rm -f $OUTF

Above shell script removes all occurrence of javascript.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 12 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
12 comments… add one
  • Sam Nov 27, 2007 @ 9:44

    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

  • Johan Jan 8, 2008 @ 10:22

    How can i make the script to look in subfolders?

  • tannu Sep 6, 2008 @ 1:06

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

  • Jasan Apr 29, 2009 @ 14:45

    tannu: you can also use “grep -v”

  • Gnaural Aug 16, 2009 @ 1:53

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

    • Lea Jan 14, 2013 @ 12:53

      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?

    • anil Oct 26, 2015 @ 20:39

      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:

      Expected output: 123[678]0.

      Thanks in advance

  • jaganath Feb 23, 2011 @ 6:46

    how can i a do electrisity bill in mysql

  • ahmed Jul 6, 2011 @ 12:27

    Great , just what i wanted!!!

  • Anderson Venturini Nov 11, 2011 @ 23:48

    Thanks! Very useful! Worked perfectly! Congrats!

  • Saeed Neamati Apr 15, 2012 @ 7:15

    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?

  • kk May 12, 2016 @ 5:22

    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 need of output file(>$OUTF).


Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum