[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How to use sed to change file contents

Posted on 2009-05-18
3
Medium Priority
?
455 Views
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

Thanks.
0
Comment
Question by:ttheimer
[X]
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
3 Comments
 
LVL 4

Accepted Solution

by:
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/
.
w
q
EOF

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

  --Eric
0
 

Author Comment

by:ttheimer
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
0
 

Author Comment

by:ttheimer
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
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

650 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