Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 28th, 2006, 5:51 AM   #1
Pimp Daddy
Programmer
 
Join Date: Aug 2004
Location: Wherever the beautiful women are.
Posts: 30
Rep Power: 0 Pimp Daddy is on a distinguished road
Send a message via MSN to Pimp Daddy Send a message via Skype™ to Pimp Daddy
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?
__________________
David Morris BSc.(Hons), MBCS
Qualified Computer Engineer
Administrator (SEED Software)
Pimp Daddy is offline   Reply With Quote
Old Aug 28th, 2006, 6:38 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,206
Rep Power: 5 grumpy is on a distinguished road
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.
grumpy is offline   Reply With Quote
Old Aug 28th, 2006, 10:19 AM   #3
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
> 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
jim mcnamara is offline   Reply With Quote
Old Sep 5th, 2006, 6:58 AM   #4
free-zombie
Programmer
 
free-zombie's Avatar
 
Join Date: May 2006
Location: Bavaria, Germany
Posts: 50
Rep Power: 0 free-zombie is an unknown quantity at this point
Send a message via ICQ to free-zombie Send a message via MSN to free-zombie Send a message via Yahoo to free-zombie
use sed -i
(edit in-place)
free-zombie is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
the video game. Burden on our future? Eric the Red Coder's Corner Lounge 41 Apr 5th, 2006 12:06 PM
Playing sounds in python? j3ff86 Python 12 Mar 30th, 2006 12:41 AM
how to run a video clip when user first run my software? jesslee Visual Basic .NET 1 Aug 7th, 2005 2:31 AM
Jackpot game zorin Visual Basic 3 Jun 10th, 2005 1:19 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:58 AM.

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