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












2















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









New contributor




mittu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    2















    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









    New contributor




    mittu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      2












      2








      2


      1






      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









      New contributor




      mittu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      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









      New contributor




      mittu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      mittu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 49 mins ago









      msp9011

      4,02843964




      4,02843964






      New contributor




      mittu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 1 hour ago









      mittumittu

      132




      132




      New contributor




      mittu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      mittu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      mittu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes


















          4














          GNU awk (commonly found on Linux systems and which you're using already as those mktime() and strftime() are GNU extensions), 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
            11 mins ago



















          1














          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
            1 hour ago











          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
          });


          }
          });






          mittu is a new contributor. Be nice, and check out our Code of Conduct.










          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









          4














          GNU awk (commonly found on Linux systems and which you're using already as those mktime() and strftime() are GNU extensions), 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
            11 mins ago
















          4














          GNU awk (commonly found on Linux systems and which you're using already as those mktime() and strftime() are GNU extensions), 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
            11 mins ago














          4












          4








          4







          GNU awk (commonly found on Linux systems and which you're using already as those mktime() and strftime() are GNU extensions), 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 using already as those mktime() and strftime() are GNU extensions), 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 29 mins ago









          Stéphane Chazelas

          302k55566918




          302k55566918










          answered 1 hour ago









          KusalanandaKusalananda

          125k16238390




          125k16238390













          • This works perfectly, Thanks a lot

            – mittu
            11 mins ago



















          • This works perfectly, Thanks a lot

            – mittu
            11 mins ago

















          This works perfectly, Thanks a lot

          – mittu
          11 mins ago





          This works perfectly, Thanks a lot

          – mittu
          11 mins ago













          1














          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
            1 hour ago
















          1














          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
            1 hour ago














          1












          1








          1







          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 1 hour ago









          msp9011msp9011

          4,02843964




          4,02843964













          • Thanks, It works

            – mittu
            1 hour ago



















          • Thanks, It works

            – mittu
            1 hour ago

















          Thanks, It works

          – mittu
          1 hour ago





          Thanks, It works

          – mittu
          1 hour ago










          mittu is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          mittu is a new contributor. Be nice, and check out our Code of Conduct.













          mittu is a new contributor. Be nice, and check out our Code of Conduct.












          mittu is a new contributor. Be nice, and check out our Code of Conduct.
















          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

          How to label and detect the document text images

          Vallis Paradisi

          Tabula Rosettana