Solved

How to use sed to change file contents

Posted on 2009-05-18
3
397 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now