[OT] Questions about editing files with sed and awk
Hey, I'm curious about something. First I should say that I haven't actually tried this with awk yet, since I don't know any, but I have tried this with sed: if you run sed on a file, sed spits the results to standard out. If you try to redirect that back into the source file, it fries it and you have an empty file. So my question is, is there a way to /edit/ a file with sed (or awk) directly, without first redirecting it to a tmp file and then catting the tmp file back into the original? P.S. Does anyone have a PDF or HTML version of O'Reilly's Sed & Awk book? ---------------------------------------------------- Jonathan Wilson System Administrator Cedar Creek Software http://www.cedarcreeksoftware.com Central Texas IT http://www.centraltexasit.com
On 1 Jun, Jonathan Wilson wrote:
So my question is, is there a way to /edit/ a file with sed (or awk) directly, without first redirecting it to a tmp file and then catting the tmp file back into the original?
The shell programs will always need a temporary file. The reason comes from how the shell handles re-direction. You can either overwrite a file (>), or append to it (>>). But you can't replace it starting at certain byte position. The overwrite erases the file first, then starts writing out new data. The shell erases the file before 'sed' ever starts. So you end up with zero bytes of data. I have a shell script that handles the creation of the tmp file, and copying it back over the original. I'd be happy to share it. Just e-mail me off the list. -- Robert Wohlfarth rjwohlfar@bigfoot.com "Is not life more important than food, and the body more important than clothes?" -- Matthew 6:25b
participants (2)
-
rjwohlfar@bigfoot.com
-
wilson@claborn.net