Solved

How to use sed to change file contents

Posted on 2009-05-18
3
425 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
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
ewest02 earned 250 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
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.

856 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