sed: input file = output file 
Author Message
 sed: input file = output file

Hello,

Is it wrong to invoke sed as:

$ cat myfile | sed -e 'script' > myfile

I did this and the file myfile ended up being cut off toward
the end even though all script did was perform a single
substitution. I presume that what I have done is catastrophic
for obvious reasons and that I should have done the following
instead:

$ cat myfile | sed -e 'script' > myfile.out
$ mv myfile.out myfile

Are my assumptions correct?
I wish it were possible to do the above in one single command.
Is it possible? (ideally there would be a command line option
like -r for replace or something like that in GNU sed which
is what I used).

Thanks,

Neil Zanella



Sun, 12 Jan 2003 03:00:00 GMT  
 sed: input file = output file

Quote:

> Is it wrong to invoke sed as:

> $ cat myfile | sed -e 'script' > myfile

Uh, let's see...

Quote:
> I did this and the file myfile ended up being cut off toward
> the end even though all script did was perform a single
> substitution. I presume that what I have done is catastrophic
> for obvious reasons and that I should have done the following
> instead:

> $ cat myfile | sed -e 'script' > myfile.out
> $ mv myfile.out myfile

> Are my assumptions correct?

Yes.

Quote:
> I wish it were possible to do the above in one single command.
> Is it possible? (ideally there would be a command line option
> like -r for replace or something like that in GNU sed which
> is what I used).

Perl has this feature and can be used in "sed mode". Use the -i
and -p command-line options together. For example:

# globally replace foo with bar in myfile, save original to myfile.bak

perl -i.bak -pe 's/foo/bar/g' myfile

--
Jim Monty

Tempe, Arizona USA



Mon, 13 Jan 2003 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. newbie would like to break input file and output to separate files

2. Reading from input file writing to output file

3. comparing an input file with an output file

4. Mutiple output files single Input file

5. Single file input ==> multi file output

6. with-input-from-file, with-output-to-file

7. with-input-from-file, with-output-to-file

8. Sed: Inserting file in another file

9. TEMPORARY FILE during input and output??

10. open-{input,output}-file

11. Text file input/output

12. reading input files and then output

 

 
Powered by phpBB® Forum Software