Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Sed and Awk (http://www.programmingforums.org/forum22.html)
-   -   Just playing (http://www.programmingforums.org/showthread.php?t=11198)

Pimp Daddy Aug 28th, 2006 5:51 AM

Just playing
 
Although I've been using Linux for a while (in various forms - such as Slackware and Cygwin), I never got around to learning the likes of grep, sed and awk. That's what I've been doing over the past few days and today I noticed something with sed. I type the following:

:

$ sed -e '/mom/s/world/hull/g' h.txt

Naturally, it replaces 'world' with 'hull' and prints the result out on the screen. However, when I try overwriting what's in the file by doing this:

:

$ sed -e '/mom/s/world/hull/g' h.txt > h.txt

it just wipes the file. What do I have to do to overwrite the contents of a file?

grumpy Aug 28th, 2006 6:38 AM

Redirect the output to another file (preferably one that doesn't exist), delete the original file, and move (or copy) the new file back in its place.

jim mcnamara Aug 28th, 2006 10:19 AM

> file
truncates (sets the file pointer to the start of an empty file) a file.
It happens before grep runs. Grumpy gave you the workaround.
:

grep 'stuff ' file1 > tmpfile
mv tmpfile file1


free-zombie Sep 5th, 2006 6:58 AM

use sed -i
(edit in-place)


All times are GMT -5. The time now is 12:29 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC