How to permanently change a file using awk? (“in-place” edits, as with “sed -i”)












8















I have an awk file new.awk



 BEGIN { FS=OFS="," }
NR==1 {
for (i=1; i<=NF; i++) {
f[$i] = i
}
}
NR > 1 {
begSecs= mktime(gensub(/[":-]/," ","g",$(f["DateTime"])))
endSecs = begSecs + $(f["TotalDuration"])
$(f["CallEndTime"]) = strftime("%Y-%m-%d %H:%M:%S", endSecs)
}
{ print }


I am calling this file in shell



awk new.awk sample.csv


But I can see the changes in the terminal, but how to make the change permanent to the file like using -i in sed.










share|improve this question





























    8















    I have an awk file new.awk



     BEGIN { FS=OFS="," }
    NR==1 {
    for (i=1; i<=NF; i++) {
    f[$i] = i
    }
    }
    NR > 1 {
    begSecs= mktime(gensub(/[":-]/," ","g",$(f["DateTime"])))
    endSecs = begSecs + $(f["TotalDuration"])
    $(f["CallEndTime"]) = strftime("%Y-%m-%d %H:%M:%S", endSecs)
    }
    { print }


    I am calling this file in shell



    awk new.awk sample.csv


    But I can see the changes in the terminal, but how to make the change permanent to the file like using -i in sed.










    share|improve this question



























      8












      8








      8


      6






      I have an awk file new.awk



       BEGIN { FS=OFS="," }
      NR==1 {
      for (i=1; i<=NF; i++) {
      f[$i] = i
      }
      }
      NR > 1 {
      begSecs= mktime(gensub(/[":-]/," ","g",$(f["DateTime"])))
      endSecs = begSecs + $(f["TotalDuration"])
      $(f["CallEndTime"]) = strftime("%Y-%m-%d %H:%M:%S", endSecs)
      }
      { print }


      I am calling this file in shell



      awk new.awk sample.csv


      But I can see the changes in the terminal, but how to make the change permanent to the file like using -i in sed.










      share|improve this question
















      I have an awk file new.awk



       BEGIN { FS=OFS="," }
      NR==1 {
      for (i=1; i<=NF; i++) {
      f[$i] = i
      }
      }
      NR > 1 {
      begSecs= mktime(gensub(/[":-]/," ","g",$(f["DateTime"])))
      endSecs = begSecs + $(f["TotalDuration"])
      $(f["CallEndTime"]) = strftime("%Y-%m-%d %H:%M:%S", endSecs)
      }
      { print }


      I am calling this file in shell



      awk new.awk sample.csv


      But I can see the changes in the terminal, but how to make the change permanent to the file like using -i in sed.







      linux shell awk






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 23 at 11:18









      msp9011

      4,37044066




      4,37044066










      asked Jan 23 at 10:44









      mittumittu

      475




      475






















          2 Answers
          2






          active

          oldest

          votes


















          12














          GNU awk (commonly found on Linux systems and which you're likely using already as those mktime() and strftime() calls are GNU extensions, also implemented by mawk, but it's less likely that this is your awk), since version 4.1.0, is able to include an "awk source library" with -i or --include on the command line. One of the source libraries that is distributed with GNU awk is one called inplace:



          $ cat file
          hello
          there




          $ awk -i inplace '/hello/ { print "oh,", $0 }' file
          $ cat file
          oh, hello


          As you can see, this makes the output of the awk code replace the input file. The line saying there is not kept as it's not outputted by the program.



          With an awk script in a file, you would use it like



          awk -i inplace -f script.awk datafile


          If the awk variable INPLACE_SUFFIX is set to a string, then the library would make a backup of the original file with that as a filename suffix.



          awk -i inplace -v INPLACE_SUFFIX=.bak -f script.awk datafile


          If you have several input files, then each file with be individually in-place edited. But you can turn in-place editing off for a file (or a set of files) by using inplace=0 on the command line before that file:



          awk -i inplace -f script.awk file1 file2 inplace=0 file3 inplace=1 file4


          In the above command, file3 would not be edited in-place.





          For a more portable "in-place edit" of a single file, use



          tmpfile=$(mktemp)
          cp file "$tmpfile" &&
          awk '...some program here...' "$tmpfile" >file
          rm "$tmpfile"


          This would copy the input file to a temporary location, then apply the awk code on the temporary file while redirecting to the original filename.



          Doing it this way ensures that file meta-data (permissions and ownership) of the original file is not modified.






          share|improve this answer


























          • This works perfectly, Thanks a lot

            – mittu
            Jan 23 at 11:56



















          4














          Try this.



          awk  new.awk sample.csv > tmp.csv && mv -f tmp.csv sample.csv



          • redirect the output to a temp file.

          • then move content of temp file to original file.






          share|improve this answer
























          • Thanks, It works

            – mittu
            Jan 23 at 11:07






          • 1





            If you look at the awk inplace source code this is exactly what it does: creates a temporary file, redirects stdout there and at the end renames it to the input file.

            – chx
            Jan 23 at 12:18











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "106"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496179%2fhow-to-permanently-change-a-file-using-awk-in-place-edits-as-with-sed-i%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          12














          GNU awk (commonly found on Linux systems and which you're likely using already as those mktime() and strftime() calls are GNU extensions, also implemented by mawk, but it's less likely that this is your awk), since version 4.1.0, is able to include an "awk source library" with -i or --include on the command line. One of the source libraries that is distributed with GNU awk is one called inplace:



          $ cat file
          hello
          there




          $ awk -i inplace '/hello/ { print "oh,", $0 }' file
          $ cat file
          oh, hello


          As you can see, this makes the output of the awk code replace the input file. The line saying there is not kept as it's not outputted by the program.



          With an awk script in a file, you would use it like



          awk -i inplace -f script.awk datafile


          If the awk variable INPLACE_SUFFIX is set to a string, then the library would make a backup of the original file with that as a filename suffix.



          awk -i inplace -v INPLACE_SUFFIX=.bak -f script.awk datafile


          If you have several input files, then each file with be individually in-place edited. But you can turn in-place editing off for a file (or a set of files) by using inplace=0 on the command line before that file:



          awk -i inplace -f script.awk file1 file2 inplace=0 file3 inplace=1 file4


          In the above command, file3 would not be edited in-place.





          For a more portable "in-place edit" of a single file, use



          tmpfile=$(mktemp)
          cp file "$tmpfile" &&
          awk '...some program here...' "$tmpfile" >file
          rm "$tmpfile"


          This would copy the input file to a temporary location, then apply the awk code on the temporary file while redirecting to the original filename.



          Doing it this way ensures that file meta-data (permissions and ownership) of the original file is not modified.






          share|improve this answer


























          • This works perfectly, Thanks a lot

            – mittu
            Jan 23 at 11:56
















          12














          GNU awk (commonly found on Linux systems and which you're likely using already as those mktime() and strftime() calls are GNU extensions, also implemented by mawk, but it's less likely that this is your awk), since version 4.1.0, is able to include an "awk source library" with -i or --include on the command line. One of the source libraries that is distributed with GNU awk is one called inplace:



          $ cat file
          hello
          there




          $ awk -i inplace '/hello/ { print "oh,", $0 }' file
          $ cat file
          oh, hello


          As you can see, this makes the output of the awk code replace the input file. The line saying there is not kept as it's not outputted by the program.



          With an awk script in a file, you would use it like



          awk -i inplace -f script.awk datafile


          If the awk variable INPLACE_SUFFIX is set to a string, then the library would make a backup of the original file with that as a filename suffix.



          awk -i inplace -v INPLACE_SUFFIX=.bak -f script.awk datafile


          If you have several input files, then each file with be individually in-place edited. But you can turn in-place editing off for a file (or a set of files) by using inplace=0 on the command line before that file:



          awk -i inplace -f script.awk file1 file2 inplace=0 file3 inplace=1 file4


          In the above command, file3 would not be edited in-place.





          For a more portable "in-place edit" of a single file, use



          tmpfile=$(mktemp)
          cp file "$tmpfile" &&
          awk '...some program here...' "$tmpfile" >file
          rm "$tmpfile"


          This would copy the input file to a temporary location, then apply the awk code on the temporary file while redirecting to the original filename.



          Doing it this way ensures that file meta-data (permissions and ownership) of the original file is not modified.






          share|improve this answer


























          • This works perfectly, Thanks a lot

            – mittu
            Jan 23 at 11:56














          12












          12








          12







          GNU awk (commonly found on Linux systems and which you're likely using already as those mktime() and strftime() calls are GNU extensions, also implemented by mawk, but it's less likely that this is your awk), since version 4.1.0, is able to include an "awk source library" with -i or --include on the command line. One of the source libraries that is distributed with GNU awk is one called inplace:



          $ cat file
          hello
          there




          $ awk -i inplace '/hello/ { print "oh,", $0 }' file
          $ cat file
          oh, hello


          As you can see, this makes the output of the awk code replace the input file. The line saying there is not kept as it's not outputted by the program.



          With an awk script in a file, you would use it like



          awk -i inplace -f script.awk datafile


          If the awk variable INPLACE_SUFFIX is set to a string, then the library would make a backup of the original file with that as a filename suffix.



          awk -i inplace -v INPLACE_SUFFIX=.bak -f script.awk datafile


          If you have several input files, then each file with be individually in-place edited. But you can turn in-place editing off for a file (or a set of files) by using inplace=0 on the command line before that file:



          awk -i inplace -f script.awk file1 file2 inplace=0 file3 inplace=1 file4


          In the above command, file3 would not be edited in-place.





          For a more portable "in-place edit" of a single file, use



          tmpfile=$(mktemp)
          cp file "$tmpfile" &&
          awk '...some program here...' "$tmpfile" >file
          rm "$tmpfile"


          This would copy the input file to a temporary location, then apply the awk code on the temporary file while redirecting to the original filename.



          Doing it this way ensures that file meta-data (permissions and ownership) of the original file is not modified.






          share|improve this answer















          GNU awk (commonly found on Linux systems and which you're likely using already as those mktime() and strftime() calls are GNU extensions, also implemented by mawk, but it's less likely that this is your awk), since version 4.1.0, is able to include an "awk source library" with -i or --include on the command line. One of the source libraries that is distributed with GNU awk is one called inplace:



          $ cat file
          hello
          there




          $ awk -i inplace '/hello/ { print "oh,", $0 }' file
          $ cat file
          oh, hello


          As you can see, this makes the output of the awk code replace the input file. The line saying there is not kept as it's not outputted by the program.



          With an awk script in a file, you would use it like



          awk -i inplace -f script.awk datafile


          If the awk variable INPLACE_SUFFIX is set to a string, then the library would make a backup of the original file with that as a filename suffix.



          awk -i inplace -v INPLACE_SUFFIX=.bak -f script.awk datafile


          If you have several input files, then each file with be individually in-place edited. But you can turn in-place editing off for a file (or a set of files) by using inplace=0 on the command line before that file:



          awk -i inplace -f script.awk file1 file2 inplace=0 file3 inplace=1 file4


          In the above command, file3 would not be edited in-place.





          For a more portable "in-place edit" of a single file, use



          tmpfile=$(mktemp)
          cp file "$tmpfile" &&
          awk '...some program here...' "$tmpfile" >file
          rm "$tmpfile"


          This would copy the input file to a temporary location, then apply the awk code on the temporary file while redirecting to the original filename.



          Doing it this way ensures that file meta-data (permissions and ownership) of the original file is not modified.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 23 at 13:06

























          answered Jan 23 at 11:00









          KusalanandaKusalananda

          133k17254417




          133k17254417













          • This works perfectly, Thanks a lot

            – mittu
            Jan 23 at 11:56



















          • This works perfectly, Thanks a lot

            – mittu
            Jan 23 at 11:56

















          This works perfectly, Thanks a lot

          – mittu
          Jan 23 at 11:56





          This works perfectly, Thanks a lot

          – mittu
          Jan 23 at 11:56













          4














          Try this.



          awk  new.awk sample.csv > tmp.csv && mv -f tmp.csv sample.csv



          • redirect the output to a temp file.

          • then move content of temp file to original file.






          share|improve this answer
























          • Thanks, It works

            – mittu
            Jan 23 at 11:07






          • 1





            If you look at the awk inplace source code this is exactly what it does: creates a temporary file, redirects stdout there and at the end renames it to the input file.

            – chx
            Jan 23 at 12:18
















          4














          Try this.



          awk  new.awk sample.csv > tmp.csv && mv -f tmp.csv sample.csv



          • redirect the output to a temp file.

          • then move content of temp file to original file.






          share|improve this answer
























          • Thanks, It works

            – mittu
            Jan 23 at 11:07






          • 1





            If you look at the awk inplace source code this is exactly what it does: creates a temporary file, redirects stdout there and at the end renames it to the input file.

            – chx
            Jan 23 at 12:18














          4












          4








          4







          Try this.



          awk  new.awk sample.csv > tmp.csv && mv -f tmp.csv sample.csv



          • redirect the output to a temp file.

          • then move content of temp file to original file.






          share|improve this answer













          Try this.



          awk  new.awk sample.csv > tmp.csv && mv -f tmp.csv sample.csv



          • redirect the output to a temp file.

          • then move content of temp file to original file.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 23 at 10:46









          msp9011msp9011

          4,37044066




          4,37044066













          • Thanks, It works

            – mittu
            Jan 23 at 11:07






          • 1





            If you look at the awk inplace source code this is exactly what it does: creates a temporary file, redirects stdout there and at the end renames it to the input file.

            – chx
            Jan 23 at 12:18



















          • Thanks, It works

            – mittu
            Jan 23 at 11:07






          • 1





            If you look at the awk inplace source code this is exactly what it does: creates a temporary file, redirects stdout there and at the end renames it to the input file.

            – chx
            Jan 23 at 12:18

















          Thanks, It works

          – mittu
          Jan 23 at 11:07





          Thanks, It works

          – mittu
          Jan 23 at 11:07




          1




          1





          If you look at the awk inplace source code this is exactly what it does: creates a temporary file, redirects stdout there and at the end renames it to the input file.

          – chx
          Jan 23 at 12:18





          If you look at the awk inplace source code this is exactly what it does: creates a temporary file, redirects stdout there and at the end renames it to the input file.

          – chx
          Jan 23 at 12:18


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Unix & Linux Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f496179%2fhow-to-permanently-change-a-file-using-awk-in-place-edits-as-with-sed-i%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Mario Kart Wii

          The Binding of Isaac: Rebirth/Afterbirth

          What does “Dominus providebit” mean?