What's happening with my sed command? [duplicate]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0
















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?










share|improve this 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.






















    0
















    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?










    share|improve this 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.




















      0












      0








      0









      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?










      share|improve this question















      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      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.






















          1 Answer
          1






          active

          oldest

          votes


















          0














          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.






          share|improve this answer























          • 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


















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          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.






          share|improve this answer























          • 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
















          0














          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.






          share|improve this answer























          • 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














          0












          0








          0







          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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


















          • 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



          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Displaying single band from multi-band raster using QGIS

          How many registers does an x86_64 CPU actually have?