What's happening with my sed command? [duplicate]
Clash Royale CLAN TAG#URR8PPP
This question already has an answer here:
Why does `cat`ing a file into itself erase it? [duplicate]
2 answers
Currently doing a little experiment in the shell.
My commands are the following :
echo 'This a cool butterfly' > test
sed 's/butterfly/parrot/g' test > test
But then when I am doing a simple cat on my test file, the file is empty. Why?
shell sed
marked as duplicate by don_crissti, Community♦ Jan 22 at 20:04
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Why does `cat`ing a file into itself erase it? [duplicate]
2 answers
Currently doing a little experiment in the shell.
My commands are the following :
echo 'This a cool butterfly' > test
sed 's/butterfly/parrot/g' test > test
But then when I am doing a simple cat on my test file, the file is empty. Why?
shell sed
marked as duplicate by don_crissti, Community♦ Jan 22 at 20:04
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Why does `cat`ing a file into itself erase it? [duplicate]
2 answers
Currently doing a little experiment in the shell.
My commands are the following :
echo 'This a cool butterfly' > test
sed 's/butterfly/parrot/g' test > test
But then when I am doing a simple cat on my test file, the file is empty. Why?
shell sed
This question already has an answer here:
Why does `cat`ing a file into itself erase it? [duplicate]
2 answers
Currently doing a little experiment in the shell.
My commands are the following :
echo 'This a cool butterfly' > test
sed 's/butterfly/parrot/g' test > test
But then when I am doing a simple cat on my test file, the file is empty. Why?
This question already has an answer here:
Why does `cat`ing a file into itself erase it? [duplicate]
2 answers
shell sed
shell sed
asked Jan 22 at 19:57
TavarichTavarich
173
173
marked as duplicate by don_crissti, Community♦ Jan 22 at 20:04
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by don_crissti, Community♦ Jan 22 at 20:04
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can't read and write to a file at the same time.
In resume, sed is reading your file 'test', but you are writing to this file in the same time, so the result is a empty file.
Try this:
sed -i 's/butterfly/parrot/g' test
With this the file will be edited in place.
The command worked, thanks. But how are my instructions executed 'at the same time'. Doesn't my command mean : take the result of the sed operation and write it (after) to the file? In this case, doesn't it work as a sort of pipe?
– Tavarich
Jan 22 at 20:02
No your command means read file test and at the same time we write to it. But before sed can start reading the file, ">" just deleted the contents to start writing data to the start of the file. So at the other hand sed started to read the file already all blank, and ">" end writing the contents as blank, as sed does not find anything anymore, and that is rewrite to the file. ">" starts writing data from the beginning. You can test that running a long time script and directing its output to a file, you can watch the file growing! "
– Luciano Andress Martini
Jan 23 at 18:24
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can't read and write to a file at the same time.
In resume, sed is reading your file 'test', but you are writing to this file in the same time, so the result is a empty file.
Try this:
sed -i 's/butterfly/parrot/g' test
With this the file will be edited in place.
The command worked, thanks. But how are my instructions executed 'at the same time'. Doesn't my command mean : take the result of the sed operation and write it (after) to the file? In this case, doesn't it work as a sort of pipe?
– Tavarich
Jan 22 at 20:02
No your command means read file test and at the same time we write to it. But before sed can start reading the file, ">" just deleted the contents to start writing data to the start of the file. So at the other hand sed started to read the file already all blank, and ">" end writing the contents as blank, as sed does not find anything anymore, and that is rewrite to the file. ">" starts writing data from the beginning. You can test that running a long time script and directing its output to a file, you can watch the file growing! "
– Luciano Andress Martini
Jan 23 at 18:24
add a comment |
You can't read and write to a file at the same time.
In resume, sed is reading your file 'test', but you are writing to this file in the same time, so the result is a empty file.
Try this:
sed -i 's/butterfly/parrot/g' test
With this the file will be edited in place.
The command worked, thanks. But how are my instructions executed 'at the same time'. Doesn't my command mean : take the result of the sed operation and write it (after) to the file? In this case, doesn't it work as a sort of pipe?
– Tavarich
Jan 22 at 20:02
No your command means read file test and at the same time we write to it. But before sed can start reading the file, ">" just deleted the contents to start writing data to the start of the file. So at the other hand sed started to read the file already all blank, and ">" end writing the contents as blank, as sed does not find anything anymore, and that is rewrite to the file. ">" starts writing data from the beginning. You can test that running a long time script and directing its output to a file, you can watch the file growing! "
– Luciano Andress Martini
Jan 23 at 18:24
add a comment |
You can't read and write to a file at the same time.
In resume, sed is reading your file 'test', but you are writing to this file in the same time, so the result is a empty file.
Try this:
sed -i 's/butterfly/parrot/g' test
With this the file will be edited in place.
You can't read and write to a file at the same time.
In resume, sed is reading your file 'test', but you are writing to this file in the same time, so the result is a empty file.
Try this:
sed -i 's/butterfly/parrot/g' test
With this the file will be edited in place.
answered Jan 22 at 20:00
Luciano Andress MartiniLuciano Andress Martini
3,883935
3,883935
The command worked, thanks. But how are my instructions executed 'at the same time'. Doesn't my command mean : take the result of the sed operation and write it (after) to the file? In this case, doesn't it work as a sort of pipe?
– Tavarich
Jan 22 at 20:02
No your command means read file test and at the same time we write to it. But before sed can start reading the file, ">" just deleted the contents to start writing data to the start of the file. So at the other hand sed started to read the file already all blank, and ">" end writing the contents as blank, as sed does not find anything anymore, and that is rewrite to the file. ">" starts writing data from the beginning. You can test that running a long time script and directing its output to a file, you can watch the file growing! "
– Luciano Andress Martini
Jan 23 at 18:24
add a comment |
The command worked, thanks. But how are my instructions executed 'at the same time'. Doesn't my command mean : take the result of the sed operation and write it (after) to the file? In this case, doesn't it work as a sort of pipe?
– Tavarich
Jan 22 at 20:02
No your command means read file test and at the same time we write to it. But before sed can start reading the file, ">" just deleted the contents to start writing data to the start of the file. So at the other hand sed started to read the file already all blank, and ">" end writing the contents as blank, as sed does not find anything anymore, and that is rewrite to the file. ">" starts writing data from the beginning. You can test that running a long time script and directing its output to a file, you can watch the file growing! "
– Luciano Andress Martini
Jan 23 at 18:24
The command worked, thanks. But how are my instructions executed 'at the same time'. Doesn't my command mean : take the result of the sed operation and write it (after) to the file? In this case, doesn't it work as a sort of pipe?
– Tavarich
Jan 22 at 20:02
The command worked, thanks. But how are my instructions executed 'at the same time'. Doesn't my command mean : take the result of the sed operation and write it (after) to the file? In this case, doesn't it work as a sort of pipe?
– Tavarich
Jan 22 at 20:02
No your command means read file test and at the same time we write to it. But before sed can start reading the file, ">" just deleted the contents to start writing data to the start of the file. So at the other hand sed started to read the file already all blank, and ">" end writing the contents as blank, as sed does not find anything anymore, and that is rewrite to the file. ">" starts writing data from the beginning. You can test that running a long time script and directing its output to a file, you can watch the file growing! "
– Luciano Andress Martini
Jan 23 at 18:24
No your command means read file test and at the same time we write to it. But before sed can start reading the file, ">" just deleted the contents to start writing data to the start of the file. So at the other hand sed started to read the file already all blank, and ">" end writing the contents as blank, as sed does not find anything anymore, and that is rewrite to the file. ">" starts writing data from the beginning. You can test that running a long time script and directing its output to a file, you can watch the file growing! "
– Luciano Andress Martini
Jan 23 at 18:24
add a comment |