How to get filenames when using find and sed

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











up vote
3
down vote

favorite












I am writing a script to apply sed on certain files and then list files that have been changed so that I know which have been modified.



This is how I am finding and then using sed:



find . -type f -a ( -name "*.txt" -o -name "*.git") -a -exec sed -i -e "s/"str1"/"str2"/g" +


How do I print the file name of the changed files? I would like to print it in a sorted order so it's easier to read.



When using only sed we can do this:



sed -i 's/$pattern/$new_pattern/w changelog.txt' $filename
if [ -s changelog.txt ]; then
# CHANGES MADE, DO SOME STUFF HERE
else
# NO CHANGES MADE, DO SOME OTHER STUFF HERE
fi


But how do I do this when using find and sed together? I checked the man page and tried a bunch of stuff but nothing worked.










share|improve this question























  • I'm not exactly sure what you're trying to achieve, can you show a list of files, and what output you're expecting to see.
    – EightBitTony
    Jan 21 '16 at 14:49










  • @EightBitTony I have added examples, please take a look
    – KLMM
    Jan 21 '16 at 17:05






  • 1




    You just add -print after your -exec, it will only be executed if the -exec was successful e.g. find . -type f ( -name *.git -o -name *.txt ) -exec sed -i 'blah_blah' ; -print. Sure, you'll have to sort the output then.
    – don_crissti
    Jan 21 '16 at 18:11










  • @don_crissti using print giving an error "-print: command not found".
    – KLMM
    Jan 21 '16 at 18:34






  • 1




    Ah, yes, sed -i is dumb and will "edit" the file even if nothing changes and report success... Add a -exec grep -q str1 ; before the existing -exec sed... That should do. Oh, and next time you reply, make sure you prepend my username with @ so the system notifies me e.g. @don_crissti otherwise I'll never know you replied (I just happened to return here)
    – don_crissti
    Jan 23 '16 at 2:45















up vote
3
down vote

favorite












I am writing a script to apply sed on certain files and then list files that have been changed so that I know which have been modified.



This is how I am finding and then using sed:



find . -type f -a ( -name "*.txt" -o -name "*.git") -a -exec sed -i -e "s/"str1"/"str2"/g" +


How do I print the file name of the changed files? I would like to print it in a sorted order so it's easier to read.



When using only sed we can do this:



sed -i 's/$pattern/$new_pattern/w changelog.txt' $filename
if [ -s changelog.txt ]; then
# CHANGES MADE, DO SOME STUFF HERE
else
# NO CHANGES MADE, DO SOME OTHER STUFF HERE
fi


But how do I do this when using find and sed together? I checked the man page and tried a bunch of stuff but nothing worked.










share|improve this question























  • I'm not exactly sure what you're trying to achieve, can you show a list of files, and what output you're expecting to see.
    – EightBitTony
    Jan 21 '16 at 14:49










  • @EightBitTony I have added examples, please take a look
    – KLMM
    Jan 21 '16 at 17:05






  • 1




    You just add -print after your -exec, it will only be executed if the -exec was successful e.g. find . -type f ( -name *.git -o -name *.txt ) -exec sed -i 'blah_blah' ; -print. Sure, you'll have to sort the output then.
    – don_crissti
    Jan 21 '16 at 18:11










  • @don_crissti using print giving an error "-print: command not found".
    – KLMM
    Jan 21 '16 at 18:34






  • 1




    Ah, yes, sed -i is dumb and will "edit" the file even if nothing changes and report success... Add a -exec grep -q str1 ; before the existing -exec sed... That should do. Oh, and next time you reply, make sure you prepend my username with @ so the system notifies me e.g. @don_crissti otherwise I'll never know you replied (I just happened to return here)
    – don_crissti
    Jan 23 '16 at 2:45













up vote
3
down vote

favorite









up vote
3
down vote

favorite











I am writing a script to apply sed on certain files and then list files that have been changed so that I know which have been modified.



This is how I am finding and then using sed:



find . -type f -a ( -name "*.txt" -o -name "*.git") -a -exec sed -i -e "s/"str1"/"str2"/g" +


How do I print the file name of the changed files? I would like to print it in a sorted order so it's easier to read.



When using only sed we can do this:



sed -i 's/$pattern/$new_pattern/w changelog.txt' $filename
if [ -s changelog.txt ]; then
# CHANGES MADE, DO SOME STUFF HERE
else
# NO CHANGES MADE, DO SOME OTHER STUFF HERE
fi


But how do I do this when using find and sed together? I checked the man page and tried a bunch of stuff but nothing worked.










share|improve this question















I am writing a script to apply sed on certain files and then list files that have been changed so that I know which have been modified.



This is how I am finding and then using sed:



find . -type f -a ( -name "*.txt" -o -name "*.git") -a -exec sed -i -e "s/"str1"/"str2"/g" +


How do I print the file name of the changed files? I would like to print it in a sorted order so it's easier to read.



When using only sed we can do this:



sed -i 's/$pattern/$new_pattern/w changelog.txt' $filename
if [ -s changelog.txt ]; then
# CHANGES MADE, DO SOME STUFF HERE
else
# NO CHANGES MADE, DO SOME OTHER STUFF HERE
fi


But how do I do this when using find and sed together? I checked the man page and tried a bunch of stuff but nothing worked.







bash shell-script sed find






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 22 '16 at 23:43

























asked Jan 21 '16 at 14:31









KLMM

59119




59119











  • I'm not exactly sure what you're trying to achieve, can you show a list of files, and what output you're expecting to see.
    – EightBitTony
    Jan 21 '16 at 14:49










  • @EightBitTony I have added examples, please take a look
    – KLMM
    Jan 21 '16 at 17:05






  • 1




    You just add -print after your -exec, it will only be executed if the -exec was successful e.g. find . -type f ( -name *.git -o -name *.txt ) -exec sed -i 'blah_blah' ; -print. Sure, you'll have to sort the output then.
    – don_crissti
    Jan 21 '16 at 18:11










  • @don_crissti using print giving an error "-print: command not found".
    – KLMM
    Jan 21 '16 at 18:34






  • 1




    Ah, yes, sed -i is dumb and will "edit" the file even if nothing changes and report success... Add a -exec grep -q str1 ; before the existing -exec sed... That should do. Oh, and next time you reply, make sure you prepend my username with @ so the system notifies me e.g. @don_crissti otherwise I'll never know you replied (I just happened to return here)
    – don_crissti
    Jan 23 '16 at 2:45

















  • I'm not exactly sure what you're trying to achieve, can you show a list of files, and what output you're expecting to see.
    – EightBitTony
    Jan 21 '16 at 14:49










  • @EightBitTony I have added examples, please take a look
    – KLMM
    Jan 21 '16 at 17:05






  • 1




    You just add -print after your -exec, it will only be executed if the -exec was successful e.g. find . -type f ( -name *.git -o -name *.txt ) -exec sed -i 'blah_blah' ; -print. Sure, you'll have to sort the output then.
    – don_crissti
    Jan 21 '16 at 18:11










  • @don_crissti using print giving an error "-print: command not found".
    – KLMM
    Jan 21 '16 at 18:34






  • 1




    Ah, yes, sed -i is dumb and will "edit" the file even if nothing changes and report success... Add a -exec grep -q str1 ; before the existing -exec sed... That should do. Oh, and next time you reply, make sure you prepend my username with @ so the system notifies me e.g. @don_crissti otherwise I'll never know you replied (I just happened to return here)
    – don_crissti
    Jan 23 '16 at 2:45
















I'm not exactly sure what you're trying to achieve, can you show a list of files, and what output you're expecting to see.
– EightBitTony
Jan 21 '16 at 14:49




I'm not exactly sure what you're trying to achieve, can you show a list of files, and what output you're expecting to see.
– EightBitTony
Jan 21 '16 at 14:49












@EightBitTony I have added examples, please take a look
– KLMM
Jan 21 '16 at 17:05




@EightBitTony I have added examples, please take a look
– KLMM
Jan 21 '16 at 17:05




1




1




You just add -print after your -exec, it will only be executed if the -exec was successful e.g. find . -type f ( -name *.git -o -name *.txt ) -exec sed -i 'blah_blah' ; -print. Sure, you'll have to sort the output then.
– don_crissti
Jan 21 '16 at 18:11




You just add -print after your -exec, it will only be executed if the -exec was successful e.g. find . -type f ( -name *.git -o -name *.txt ) -exec sed -i 'blah_blah' ; -print. Sure, you'll have to sort the output then.
– don_crissti
Jan 21 '16 at 18:11












@don_crissti using print giving an error "-print: command not found".
– KLMM
Jan 21 '16 at 18:34




@don_crissti using print giving an error "-print: command not found".
– KLMM
Jan 21 '16 at 18:34




1




1




Ah, yes, sed -i is dumb and will "edit" the file even if nothing changes and report success... Add a -exec grep -q str1 ; before the existing -exec sed... That should do. Oh, and next time you reply, make sure you prepend my username with @ so the system notifies me e.g. @don_crissti otherwise I'll never know you replied (I just happened to return here)
– don_crissti
Jan 23 '16 at 2:45





Ah, yes, sed -i is dumb and will "edit" the file even if nothing changes and report success... Add a -exec grep -q str1 ; before the existing -exec sed... That should do. Oh, and next time you reply, make sure you prepend my username with @ so the system notifies me e.g. @don_crissti otherwise I'll never know you replied (I just happened to return here)
– don_crissti
Jan 23 '16 at 2:45











3 Answers
3






active

oldest

votes

















up vote
0
down vote













It should be easy enough to write a little script that does what you want and exec the script as an argument to find. You already have the script and if you replace $filename by $1, you have it. Your script will be of the form



#!/bin/bash
sed -i 's/$pattern/$new_pattern/' $1
echo $1 >> changelog


Let us call this script ed_notify. Now, you can run it on selected files by



cat changelog >> changelog.old
rm changelog
find . -type f -a ( -name "*.txt" -o -name "*.git") -a -exec ed_notify ;





share|improve this answer


















  • 2




    Please edit your answer so that it actually provides an answer. At the moment, this is a comment simply giving a suggestion.
    – terdon♦
    Jan 21 '16 at 15:06










  • @unxnut I am unable to understand your answer, I do have a high level idea of what needs to be done. Please provide some code solution, thanks
    – KLMM
    Jan 21 '16 at 17:06










  • @unxnut Is it possible to achieve this in a single script(will using routines work)? And how do we get sorted order?
    – KLMM
    Jan 21 '16 at 20:31










  • Since the filenames for the session are saved in changelog, all you have to do is sort changelog to get the file in sorted order at the end of the find command.
    – unxnut
    Jan 21 '16 at 21:11

















up vote
0
down vote













Your sed command (with proper quoting):



sed 's/str1/str2/g'


This will change all occurrences of str1 into str2. A list of files containing str1 can be had from grep -l 'str1':



find . -type f ( -name '*.txt' -o -name '*.git ) 
-exec grep -l 'str1' ;
-exec sed -i 's/str1/str2/g' + >changelist.txt


Here, grep -l will provide a list of pathnames that will be redirected into changelist.txt. It will also act like a filter for sed so that sed is only run on files that contain the pattern. sed -i will then make the changes in the files (and remain quiet).



Alternatively, let find print the pathnames of the files that contain the string:



find . -type f ( -name '*.txt' -o -name '*.git ) 
-exec grep -q 'str1' ;
-print
-exec sed -i 's/str1/str2/g' + >changelist.txt


Related:



  • Understanding the -exec option of `find`





share|improve this answer





























    up vote
    0
    down vote













    sed -i rewrites the file (actually makes full new copies of the files) regardless of whether any of the s commands in the sed script succeeded or not.



    Here, you'd want to avoid running sed -i on files that don't contain str1. With GNU tools:



    find . -type f ( -name "*.txt" -o -name "*.git" ) -size +3c 
    -exec grep -lZ str1 + |
    while IFS= read -rd '' file; do
    sed -i 's/str1/str2/g' "$file" &&
    printf '%sn' "$file"
    done


    That runs one sed for each of the files that contain str1 and prints the file names if sed has been successful (for which there has been no error in creating the new version of the file).



    Or you can run one grep and sed per file:



    find . -type f ( -name "*.txt" -o -name "*.git" ) ( -size +3c 
    -exec grep -q str1 ;
    -exec sed -i 's/str1/str2/g' ;
    -printf '"%p" was modifiedn'
    -o -printf '"%p" was not modifiedn"' )





    share|improve this answer




















      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',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      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%2f256791%2fhow-to-get-filenames-when-using-find-and-sed%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      It should be easy enough to write a little script that does what you want and exec the script as an argument to find. You already have the script and if you replace $filename by $1, you have it. Your script will be of the form



      #!/bin/bash
      sed -i 's/$pattern/$new_pattern/' $1
      echo $1 >> changelog


      Let us call this script ed_notify. Now, you can run it on selected files by



      cat changelog >> changelog.old
      rm changelog
      find . -type f -a ( -name "*.txt" -o -name "*.git") -a -exec ed_notify ;





      share|improve this answer


















      • 2




        Please edit your answer so that it actually provides an answer. At the moment, this is a comment simply giving a suggestion.
        – terdon♦
        Jan 21 '16 at 15:06










      • @unxnut I am unable to understand your answer, I do have a high level idea of what needs to be done. Please provide some code solution, thanks
        – KLMM
        Jan 21 '16 at 17:06










      • @unxnut Is it possible to achieve this in a single script(will using routines work)? And how do we get sorted order?
        – KLMM
        Jan 21 '16 at 20:31










      • Since the filenames for the session are saved in changelog, all you have to do is sort changelog to get the file in sorted order at the end of the find command.
        – unxnut
        Jan 21 '16 at 21:11














      up vote
      0
      down vote













      It should be easy enough to write a little script that does what you want and exec the script as an argument to find. You already have the script and if you replace $filename by $1, you have it. Your script will be of the form



      #!/bin/bash
      sed -i 's/$pattern/$new_pattern/' $1
      echo $1 >> changelog


      Let us call this script ed_notify. Now, you can run it on selected files by



      cat changelog >> changelog.old
      rm changelog
      find . -type f -a ( -name "*.txt" -o -name "*.git") -a -exec ed_notify ;





      share|improve this answer


















      • 2




        Please edit your answer so that it actually provides an answer. At the moment, this is a comment simply giving a suggestion.
        – terdon♦
        Jan 21 '16 at 15:06










      • @unxnut I am unable to understand your answer, I do have a high level idea of what needs to be done. Please provide some code solution, thanks
        – KLMM
        Jan 21 '16 at 17:06










      • @unxnut Is it possible to achieve this in a single script(will using routines work)? And how do we get sorted order?
        – KLMM
        Jan 21 '16 at 20:31










      • Since the filenames for the session are saved in changelog, all you have to do is sort changelog to get the file in sorted order at the end of the find command.
        – unxnut
        Jan 21 '16 at 21:11












      up vote
      0
      down vote










      up vote
      0
      down vote









      It should be easy enough to write a little script that does what you want and exec the script as an argument to find. You already have the script and if you replace $filename by $1, you have it. Your script will be of the form



      #!/bin/bash
      sed -i 's/$pattern/$new_pattern/' $1
      echo $1 >> changelog


      Let us call this script ed_notify. Now, you can run it on selected files by



      cat changelog >> changelog.old
      rm changelog
      find . -type f -a ( -name "*.txt" -o -name "*.git") -a -exec ed_notify ;





      share|improve this answer














      It should be easy enough to write a little script that does what you want and exec the script as an argument to find. You already have the script and if you replace $filename by $1, you have it. Your script will be of the form



      #!/bin/bash
      sed -i 's/$pattern/$new_pattern/' $1
      echo $1 >> changelog


      Let us call this script ed_notify. Now, you can run it on selected files by



      cat changelog >> changelog.old
      rm changelog
      find . -type f -a ( -name "*.txt" -o -name "*.git") -a -exec ed_notify ;






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jan 21 '16 at 18:59

























      answered Jan 21 '16 at 14:51









      unxnut

      3,4202918




      3,4202918







      • 2




        Please edit your answer so that it actually provides an answer. At the moment, this is a comment simply giving a suggestion.
        – terdon♦
        Jan 21 '16 at 15:06










      • @unxnut I am unable to understand your answer, I do have a high level idea of what needs to be done. Please provide some code solution, thanks
        – KLMM
        Jan 21 '16 at 17:06










      • @unxnut Is it possible to achieve this in a single script(will using routines work)? And how do we get sorted order?
        – KLMM
        Jan 21 '16 at 20:31










      • Since the filenames for the session are saved in changelog, all you have to do is sort changelog to get the file in sorted order at the end of the find command.
        – unxnut
        Jan 21 '16 at 21:11












      • 2




        Please edit your answer so that it actually provides an answer. At the moment, this is a comment simply giving a suggestion.
        – terdon♦
        Jan 21 '16 at 15:06










      • @unxnut I am unable to understand your answer, I do have a high level idea of what needs to be done. Please provide some code solution, thanks
        – KLMM
        Jan 21 '16 at 17:06










      • @unxnut Is it possible to achieve this in a single script(will using routines work)? And how do we get sorted order?
        – KLMM
        Jan 21 '16 at 20:31










      • Since the filenames for the session are saved in changelog, all you have to do is sort changelog to get the file in sorted order at the end of the find command.
        – unxnut
        Jan 21 '16 at 21:11







      2




      2




      Please edit your answer so that it actually provides an answer. At the moment, this is a comment simply giving a suggestion.
      – terdon♦
      Jan 21 '16 at 15:06




      Please edit your answer so that it actually provides an answer. At the moment, this is a comment simply giving a suggestion.
      – terdon♦
      Jan 21 '16 at 15:06












      @unxnut I am unable to understand your answer, I do have a high level idea of what needs to be done. Please provide some code solution, thanks
      – KLMM
      Jan 21 '16 at 17:06




      @unxnut I am unable to understand your answer, I do have a high level idea of what needs to be done. Please provide some code solution, thanks
      – KLMM
      Jan 21 '16 at 17:06












      @unxnut Is it possible to achieve this in a single script(will using routines work)? And how do we get sorted order?
      – KLMM
      Jan 21 '16 at 20:31




      @unxnut Is it possible to achieve this in a single script(will using routines work)? And how do we get sorted order?
      – KLMM
      Jan 21 '16 at 20:31












      Since the filenames for the session are saved in changelog, all you have to do is sort changelog to get the file in sorted order at the end of the find command.
      – unxnut
      Jan 21 '16 at 21:11




      Since the filenames for the session are saved in changelog, all you have to do is sort changelog to get the file in sorted order at the end of the find command.
      – unxnut
      Jan 21 '16 at 21:11












      up vote
      0
      down vote













      Your sed command (with proper quoting):



      sed 's/str1/str2/g'


      This will change all occurrences of str1 into str2. A list of files containing str1 can be had from grep -l 'str1':



      find . -type f ( -name '*.txt' -o -name '*.git ) 
      -exec grep -l 'str1' ;
      -exec sed -i 's/str1/str2/g' + >changelist.txt


      Here, grep -l will provide a list of pathnames that will be redirected into changelist.txt. It will also act like a filter for sed so that sed is only run on files that contain the pattern. sed -i will then make the changes in the files (and remain quiet).



      Alternatively, let find print the pathnames of the files that contain the string:



      find . -type f ( -name '*.txt' -o -name '*.git ) 
      -exec grep -q 'str1' ;
      -print
      -exec sed -i 's/str1/str2/g' + >changelist.txt


      Related:



      • Understanding the -exec option of `find`





      share|improve this answer


























        up vote
        0
        down vote













        Your sed command (with proper quoting):



        sed 's/str1/str2/g'


        This will change all occurrences of str1 into str2. A list of files containing str1 can be had from grep -l 'str1':



        find . -type f ( -name '*.txt' -o -name '*.git ) 
        -exec grep -l 'str1' ;
        -exec sed -i 's/str1/str2/g' + >changelist.txt


        Here, grep -l will provide a list of pathnames that will be redirected into changelist.txt. It will also act like a filter for sed so that sed is only run on files that contain the pattern. sed -i will then make the changes in the files (and remain quiet).



        Alternatively, let find print the pathnames of the files that contain the string:



        find . -type f ( -name '*.txt' -o -name '*.git ) 
        -exec grep -q 'str1' ;
        -print
        -exec sed -i 's/str1/str2/g' + >changelist.txt


        Related:



        • Understanding the -exec option of `find`





        share|improve this answer
























          up vote
          0
          down vote










          up vote
          0
          down vote









          Your sed command (with proper quoting):



          sed 's/str1/str2/g'


          This will change all occurrences of str1 into str2. A list of files containing str1 can be had from grep -l 'str1':



          find . -type f ( -name '*.txt' -o -name '*.git ) 
          -exec grep -l 'str1' ;
          -exec sed -i 's/str1/str2/g' + >changelist.txt


          Here, grep -l will provide a list of pathnames that will be redirected into changelist.txt. It will also act like a filter for sed so that sed is only run on files that contain the pattern. sed -i will then make the changes in the files (and remain quiet).



          Alternatively, let find print the pathnames of the files that contain the string:



          find . -type f ( -name '*.txt' -o -name '*.git ) 
          -exec grep -q 'str1' ;
          -print
          -exec sed -i 's/str1/str2/g' + >changelist.txt


          Related:



          • Understanding the -exec option of `find`





          share|improve this answer














          Your sed command (with proper quoting):



          sed 's/str1/str2/g'


          This will change all occurrences of str1 into str2. A list of files containing str1 can be had from grep -l 'str1':



          find . -type f ( -name '*.txt' -o -name '*.git ) 
          -exec grep -l 'str1' ;
          -exec sed -i 's/str1/str2/g' + >changelist.txt


          Here, grep -l will provide a list of pathnames that will be redirected into changelist.txt. It will also act like a filter for sed so that sed is only run on files that contain the pattern. sed -i will then make the changes in the files (and remain quiet).



          Alternatively, let find print the pathnames of the files that contain the string:



          find . -type f ( -name '*.txt' -o -name '*.git ) 
          -exec grep -q 'str1' ;
          -print
          -exec sed -i 's/str1/str2/g' + >changelist.txt


          Related:



          • Understanding the -exec option of `find`






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 5 at 12:08

























          answered Sep 23 '17 at 14:39









          Kusalananda

          107k14209331




          107k14209331




















              up vote
              0
              down vote













              sed -i rewrites the file (actually makes full new copies of the files) regardless of whether any of the s commands in the sed script succeeded or not.



              Here, you'd want to avoid running sed -i on files that don't contain str1. With GNU tools:



              find . -type f ( -name "*.txt" -o -name "*.git" ) -size +3c 
              -exec grep -lZ str1 + |
              while IFS= read -rd '' file; do
              sed -i 's/str1/str2/g' "$file" &&
              printf '%sn' "$file"
              done


              That runs one sed for each of the files that contain str1 and prints the file names if sed has been successful (for which there has been no error in creating the new version of the file).



              Or you can run one grep and sed per file:



              find . -type f ( -name "*.txt" -o -name "*.git" ) ( -size +3c 
              -exec grep -q str1 ;
              -exec sed -i 's/str1/str2/g' ;
              -printf '"%p" was modifiedn'
              -o -printf '"%p" was not modifiedn"' )





              share|improve this answer
























                up vote
                0
                down vote













                sed -i rewrites the file (actually makes full new copies of the files) regardless of whether any of the s commands in the sed script succeeded or not.



                Here, you'd want to avoid running sed -i on files that don't contain str1. With GNU tools:



                find . -type f ( -name "*.txt" -o -name "*.git" ) -size +3c 
                -exec grep -lZ str1 + |
                while IFS= read -rd '' file; do
                sed -i 's/str1/str2/g' "$file" &&
                printf '%sn' "$file"
                done


                That runs one sed for each of the files that contain str1 and prints the file names if sed has been successful (for which there has been no error in creating the new version of the file).



                Or you can run one grep and sed per file:



                find . -type f ( -name "*.txt" -o -name "*.git" ) ( -size +3c 
                -exec grep -q str1 ;
                -exec sed -i 's/str1/str2/g' ;
                -printf '"%p" was modifiedn'
                -o -printf '"%p" was not modifiedn"' )





                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  sed -i rewrites the file (actually makes full new copies of the files) regardless of whether any of the s commands in the sed script succeeded or not.



                  Here, you'd want to avoid running sed -i on files that don't contain str1. With GNU tools:



                  find . -type f ( -name "*.txt" -o -name "*.git" ) -size +3c 
                  -exec grep -lZ str1 + |
                  while IFS= read -rd '' file; do
                  sed -i 's/str1/str2/g' "$file" &&
                  printf '%sn' "$file"
                  done


                  That runs one sed for each of the files that contain str1 and prints the file names if sed has been successful (for which there has been no error in creating the new version of the file).



                  Or you can run one grep and sed per file:



                  find . -type f ( -name "*.txt" -o -name "*.git" ) ( -size +3c 
                  -exec grep -q str1 ;
                  -exec sed -i 's/str1/str2/g' ;
                  -printf '"%p" was modifiedn'
                  -o -printf '"%p" was not modifiedn"' )





                  share|improve this answer












                  sed -i rewrites the file (actually makes full new copies of the files) regardless of whether any of the s commands in the sed script succeeded or not.



                  Here, you'd want to avoid running sed -i on files that don't contain str1. With GNU tools:



                  find . -type f ( -name "*.txt" -o -name "*.git" ) -size +3c 
                  -exec grep -lZ str1 + |
                  while IFS= read -rd '' file; do
                  sed -i 's/str1/str2/g' "$file" &&
                  printf '%sn' "$file"
                  done


                  That runs one sed for each of the files that contain str1 and prints the file names if sed has been successful (for which there has been no error in creating the new version of the file).



                  Or you can run one grep and sed per file:



                  find . -type f ( -name "*.txt" -o -name "*.git" ) ( -size +3c 
                  -exec grep -q str1 ;
                  -exec sed -i 's/str1/str2/g' ;
                  -printf '"%p" was modifiedn'
                  -o -printf '"%p" was not modifiedn"' )






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 5 at 12:11









                  Stéphane Chazelas

                  286k53528866




                  286k53528866



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f256791%2fhow-to-get-filenames-when-using-find-and-sed%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      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?