Re: I/O redirection problem
From: Stephane CHAZELAS (this.address_at_is.invalid)
Date: 05/26/04
- Next message: Stephane CHAZELAS: "Re: I/O redirection problem"
- Previous message: Chris F.A. Johnson: "Re: I/O redirection problem"
- In reply to: Krit Kasemosoth: "I/O redirection problem"
- Next in thread: Stephane CHAZELAS: "Re: I/O redirection problem"
- Reply: Stephane CHAZELAS: "Re: I/O redirection problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 26 May 2004 12:54:38 +0000
2004-05-26, 04:32(-07), Krit Kasemosoth:
> I have data file 'test.txt'. I want to search text in that file and
> store
> the result back to file 'test.txt'. The command that I execute is :
>
> cat test.txt | grep monday > test.txt
[...]
That's a common error.
"cat test.txt" and "grep monday > test.txt" are two commands run
at the same time. The right one opens the test.txt file with
truncation, so reduces it to an empty file. If the left one has
not opened test.txt for reading before, it sees an empty file.
Even if it opened it before, you may have unexpected results.
You shouldn't read and write to a same file at the same time.
Use a temporary file or make so that the file written to is a
new instance of the same file:
{ rm test.txt && grep monday > test.txt; } < test.txt
(Here, the former test.txt is opened for reading, then removed,
and a new one is opened for writing).
Alternatively, you can use:
perl -ni -e 'print if /monday/' test.txt
or
ex -s -c 'v/monday/d|wq' test.txt
(none tested).
-- Stephane
- Next message: Stephane CHAZELAS: "Re: I/O redirection problem"
- Previous message: Chris F.A. Johnson: "Re: I/O redirection problem"
- In reply to: Krit Kasemosoth: "I/O redirection problem"
- Next in thread: Stephane CHAZELAS: "Re: I/O redirection problem"
- Reply: Stephane CHAZELAS: "Re: I/O redirection problem"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|