How to use sed to change file contents

Posted on 2009-05-18
Medium Priority
Last Modified: 2013-12-16
New to sed... I have the following statement that when entered on the command line works properly to make the desired replacement.  This version sends the result to stdout.  I can also redirect to a new file by appending ">newfile".  But I want to change the original file and when I redirect to the same file name I get an empty file.  What am I missing?  How do I change the original file?

sed '/^<script language=javascript><\!-- /,/^ -->/c<body>' sedtesttarget.html

Question by:ttheimer
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

ewest02 earned 1000 total points
ID: 24415321
GNU sed has a --in-place (or -i) option to read/write the named file

Alternatively, you can use ed
The general approach is to use cat with a HERE directive to pass directives to ed

cat <<EOF | ed myfile
1,$s/some text/SOME TEXT/

In this example, open "myfile" and replace "some text" with "SOME TEXT".
The directives for ed overlap with those for sed -- they are related.


Author Comment

ID: 24415484
The --in-place (or -i) option was not noted in my [paper] reference text - found it in the man file.  Works perfectly.  Thanks.

New command:

sed --in-place '/^<script language=javascript><\!-- /,/^ -->/c<body>' sedtesttarget.html

Author Comment

ID: 24415485
The --in-place (or -i) option was not noted in my [paper] reference text - found it in the man file.  Works perfectly.  Thanks.

New command:

sed --in-place '/^<script language=javascript><\!-- /,/^ -->/c<body>' sedtesttarget.html

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question