Replace string with spaces in entire directory

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











up vote
0
down vote

favorite












I want to replace 'Aux Power [A]' in all files to 'aux_power'.
Only the files with 'Aux Power [A]' can be modified, the rest need to remain untouched.


I have tried



grep -lR "Aux Power [A]" /mnt/d/power/jan/output | xargs sed -i "s/'Aux Power [A]'/'aux_power_w'/g"


but it doesn't seem to work.


Please advice.







share|improve this question


















  • 1




    Just to clarify: You wish to change file contents, do you?
    – Ned64
    Feb 17 at 11:11














up vote
0
down vote

favorite












I want to replace 'Aux Power [A]' in all files to 'aux_power'.
Only the files with 'Aux Power [A]' can be modified, the rest need to remain untouched.


I have tried



grep -lR "Aux Power [A]" /mnt/d/power/jan/output | xargs sed -i "s/'Aux Power [A]'/'aux_power_w'/g"


but it doesn't seem to work.


Please advice.







share|improve this question


















  • 1




    Just to clarify: You wish to change file contents, do you?
    – Ned64
    Feb 17 at 11:11












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to replace 'Aux Power [A]' in all files to 'aux_power'.
Only the files with 'Aux Power [A]' can be modified, the rest need to remain untouched.


I have tried



grep -lR "Aux Power [A]" /mnt/d/power/jan/output | xargs sed -i "s/'Aux Power [A]'/'aux_power_w'/g"


but it doesn't seem to work.


Please advice.







share|improve this question














I want to replace 'Aux Power [A]' in all files to 'aux_power'.
Only the files with 'Aux Power [A]' can be modified, the rest need to remain untouched.


I have tried



grep -lR "Aux Power [A]" /mnt/d/power/jan/output | xargs sed -i "s/'Aux Power [A]'/'aux_power_w'/g"


but it doesn't seem to work.


Please advice.









share|improve this question













share|improve this question




share|improve this question








edited Feb 17 at 12:13









Jeff Schaller

31.2k846105




31.2k846105










asked Feb 17 at 10:51









Umer

1




1







  • 1




    Just to clarify: You wish to change file contents, do you?
    – Ned64
    Feb 17 at 11:11












  • 1




    Just to clarify: You wish to change file contents, do you?
    – Ned64
    Feb 17 at 11:11







1




1




Just to clarify: You wish to change file contents, do you?
– Ned64
Feb 17 at 11:11




Just to clarify: You wish to change file contents, do you?
– Ned64
Feb 17 at 11:11










2 Answers
2






active

oldest

votes

















up vote
2
down vote













The issue with your code is twofold:



  1. The grep command, by default, expects a regular expression pattern, not a string, and your string contains characters special to regular expressions ([ and ]). You have the same issue in your sed substitution.


  2. Filenames outputted by grep -l may be mangled. If a file contains an unprintable character (such as newline), grep will simply drop that from the outputted name. This means that sed won't be invoked on that file.



This is assuming you'd like to replace the string within the file content, not in filenames:



To find all files beneath the directory /mnt/d/power/jan/output that contains the exact string Aux Power [A], you would use



find /mnt/d/power/jan/output -type f 
-exec grep -Fq 'Aux Power [A]' ';' -print


To change the string to aux_power you would extend that command like so:



find /mnt/d/power/jan/output -type f 
-exec grep -Fq 'Aux Power [A]' ';'
-exec sed -i 's/Aux Power [A]/aux_power/g' ';'


This is assuming GNU sed.



The grep -Fq will not produce any output but will return a zero exit status for any file that contains the specified string (not regular expression since -F is used).



The sed command does in-place editing (the GNU way), changing all occurrences of the string (here specified as a regular expression, which is why the brackets are escaped).



If you'd like to get the names of the modified files listed in the terminal, then leave the -print in from the first find command, after the -exec grep ... ';'.




Since the sed expression that we're using here won't change the contents of a file that does not contain the particular string you want to change, you may shorten the command down to



find /mnt/d/power/jan/output -type f 
-exec sed -i 's/Aux Power [A]/aux_power/g' ';'


This applies the substitution to all files under the /mnt/d/power/jan/output directory. Any file that does not contain the string will remain unchanged (but its timestamp would still be updated).






share|improve this answer






















  • You worry about file name mangling but fail to get the replacement right. Matthew 7:3.
    – Gerard H. Pille
    Feb 17 at 17:38










  • @GerardH.Pille Please explain and I will get it right.
    – Kusalananda
    Feb 17 at 17:50










  • @GerardH.Pille If you referring to the string aux_power_w, then all I can say is that the question asks to replace with aux_power while his attempt is to replace with aux_power_w. The attempt don't match the question, and it's uncertain which one is actually meant. If I have another error in my code, I'd be more than willing to correct it.
    – Kusalananda
    Feb 17 at 18:13

















up vote
1
down vote













You should escape the square brackets for grep. "[A]" is the same as "A".
Try



grep -lR "Aux Power [A]" /mnt/d/power/jan/output | xargs sed -i "s/Aux Power [A]/aux_power_w/g"





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%2f424761%2freplace-string-with-spaces-in-entire-directory%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    The issue with your code is twofold:



    1. The grep command, by default, expects a regular expression pattern, not a string, and your string contains characters special to regular expressions ([ and ]). You have the same issue in your sed substitution.


    2. Filenames outputted by grep -l may be mangled. If a file contains an unprintable character (such as newline), grep will simply drop that from the outputted name. This means that sed won't be invoked on that file.



    This is assuming you'd like to replace the string within the file content, not in filenames:



    To find all files beneath the directory /mnt/d/power/jan/output that contains the exact string Aux Power [A], you would use



    find /mnt/d/power/jan/output -type f 
    -exec grep -Fq 'Aux Power [A]' ';' -print


    To change the string to aux_power you would extend that command like so:



    find /mnt/d/power/jan/output -type f 
    -exec grep -Fq 'Aux Power [A]' ';'
    -exec sed -i 's/Aux Power [A]/aux_power/g' ';'


    This is assuming GNU sed.



    The grep -Fq will not produce any output but will return a zero exit status for any file that contains the specified string (not regular expression since -F is used).



    The sed command does in-place editing (the GNU way), changing all occurrences of the string (here specified as a regular expression, which is why the brackets are escaped).



    If you'd like to get the names of the modified files listed in the terminal, then leave the -print in from the first find command, after the -exec grep ... ';'.




    Since the sed expression that we're using here won't change the contents of a file that does not contain the particular string you want to change, you may shorten the command down to



    find /mnt/d/power/jan/output -type f 
    -exec sed -i 's/Aux Power [A]/aux_power/g' ';'


    This applies the substitution to all files under the /mnt/d/power/jan/output directory. Any file that does not contain the string will remain unchanged (but its timestamp would still be updated).






    share|improve this answer






















    • You worry about file name mangling but fail to get the replacement right. Matthew 7:3.
      – Gerard H. Pille
      Feb 17 at 17:38










    • @GerardH.Pille Please explain and I will get it right.
      – Kusalananda
      Feb 17 at 17:50










    • @GerardH.Pille If you referring to the string aux_power_w, then all I can say is that the question asks to replace with aux_power while his attempt is to replace with aux_power_w. The attempt don't match the question, and it's uncertain which one is actually meant. If I have another error in my code, I'd be more than willing to correct it.
      – Kusalananda
      Feb 17 at 18:13














    up vote
    2
    down vote













    The issue with your code is twofold:



    1. The grep command, by default, expects a regular expression pattern, not a string, and your string contains characters special to regular expressions ([ and ]). You have the same issue in your sed substitution.


    2. Filenames outputted by grep -l may be mangled. If a file contains an unprintable character (such as newline), grep will simply drop that from the outputted name. This means that sed won't be invoked on that file.



    This is assuming you'd like to replace the string within the file content, not in filenames:



    To find all files beneath the directory /mnt/d/power/jan/output that contains the exact string Aux Power [A], you would use



    find /mnt/d/power/jan/output -type f 
    -exec grep -Fq 'Aux Power [A]' ';' -print


    To change the string to aux_power you would extend that command like so:



    find /mnt/d/power/jan/output -type f 
    -exec grep -Fq 'Aux Power [A]' ';'
    -exec sed -i 's/Aux Power [A]/aux_power/g' ';'


    This is assuming GNU sed.



    The grep -Fq will not produce any output but will return a zero exit status for any file that contains the specified string (not regular expression since -F is used).



    The sed command does in-place editing (the GNU way), changing all occurrences of the string (here specified as a regular expression, which is why the brackets are escaped).



    If you'd like to get the names of the modified files listed in the terminal, then leave the -print in from the first find command, after the -exec grep ... ';'.




    Since the sed expression that we're using here won't change the contents of a file that does not contain the particular string you want to change, you may shorten the command down to



    find /mnt/d/power/jan/output -type f 
    -exec sed -i 's/Aux Power [A]/aux_power/g' ';'


    This applies the substitution to all files under the /mnt/d/power/jan/output directory. Any file that does not contain the string will remain unchanged (but its timestamp would still be updated).






    share|improve this answer






















    • You worry about file name mangling but fail to get the replacement right. Matthew 7:3.
      – Gerard H. Pille
      Feb 17 at 17:38










    • @GerardH.Pille Please explain and I will get it right.
      – Kusalananda
      Feb 17 at 17:50










    • @GerardH.Pille If you referring to the string aux_power_w, then all I can say is that the question asks to replace with aux_power while his attempt is to replace with aux_power_w. The attempt don't match the question, and it's uncertain which one is actually meant. If I have another error in my code, I'd be more than willing to correct it.
      – Kusalananda
      Feb 17 at 18:13












    up vote
    2
    down vote










    up vote
    2
    down vote









    The issue with your code is twofold:



    1. The grep command, by default, expects a regular expression pattern, not a string, and your string contains characters special to regular expressions ([ and ]). You have the same issue in your sed substitution.


    2. Filenames outputted by grep -l may be mangled. If a file contains an unprintable character (such as newline), grep will simply drop that from the outputted name. This means that sed won't be invoked on that file.



    This is assuming you'd like to replace the string within the file content, not in filenames:



    To find all files beneath the directory /mnt/d/power/jan/output that contains the exact string Aux Power [A], you would use



    find /mnt/d/power/jan/output -type f 
    -exec grep -Fq 'Aux Power [A]' ';' -print


    To change the string to aux_power you would extend that command like so:



    find /mnt/d/power/jan/output -type f 
    -exec grep -Fq 'Aux Power [A]' ';'
    -exec sed -i 's/Aux Power [A]/aux_power/g' ';'


    This is assuming GNU sed.



    The grep -Fq will not produce any output but will return a zero exit status for any file that contains the specified string (not regular expression since -F is used).



    The sed command does in-place editing (the GNU way), changing all occurrences of the string (here specified as a regular expression, which is why the brackets are escaped).



    If you'd like to get the names of the modified files listed in the terminal, then leave the -print in from the first find command, after the -exec grep ... ';'.




    Since the sed expression that we're using here won't change the contents of a file that does not contain the particular string you want to change, you may shorten the command down to



    find /mnt/d/power/jan/output -type f 
    -exec sed -i 's/Aux Power [A]/aux_power/g' ';'


    This applies the substitution to all files under the /mnt/d/power/jan/output directory. Any file that does not contain the string will remain unchanged (but its timestamp would still be updated).






    share|improve this answer














    The issue with your code is twofold:



    1. The grep command, by default, expects a regular expression pattern, not a string, and your string contains characters special to regular expressions ([ and ]). You have the same issue in your sed substitution.


    2. Filenames outputted by grep -l may be mangled. If a file contains an unprintable character (such as newline), grep will simply drop that from the outputted name. This means that sed won't be invoked on that file.



    This is assuming you'd like to replace the string within the file content, not in filenames:



    To find all files beneath the directory /mnt/d/power/jan/output that contains the exact string Aux Power [A], you would use



    find /mnt/d/power/jan/output -type f 
    -exec grep -Fq 'Aux Power [A]' ';' -print


    To change the string to aux_power you would extend that command like so:



    find /mnt/d/power/jan/output -type f 
    -exec grep -Fq 'Aux Power [A]' ';'
    -exec sed -i 's/Aux Power [A]/aux_power/g' ';'


    This is assuming GNU sed.



    The grep -Fq will not produce any output but will return a zero exit status for any file that contains the specified string (not regular expression since -F is used).



    The sed command does in-place editing (the GNU way), changing all occurrences of the string (here specified as a regular expression, which is why the brackets are escaped).



    If you'd like to get the names of the modified files listed in the terminal, then leave the -print in from the first find command, after the -exec grep ... ';'.




    Since the sed expression that we're using here won't change the contents of a file that does not contain the particular string you want to change, you may shorten the command down to



    find /mnt/d/power/jan/output -type f 
    -exec sed -i 's/Aux Power [A]/aux_power/g' ';'


    This applies the substitution to all files under the /mnt/d/power/jan/output directory. Any file that does not contain the string will remain unchanged (but its timestamp would still be updated).







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Feb 17 at 14:40

























    answered Feb 17 at 11:23









    Kusalananda

    103k13202318




    103k13202318











    • You worry about file name mangling but fail to get the replacement right. Matthew 7:3.
      – Gerard H. Pille
      Feb 17 at 17:38










    • @GerardH.Pille Please explain and I will get it right.
      – Kusalananda
      Feb 17 at 17:50










    • @GerardH.Pille If you referring to the string aux_power_w, then all I can say is that the question asks to replace with aux_power while his attempt is to replace with aux_power_w. The attempt don't match the question, and it's uncertain which one is actually meant. If I have another error in my code, I'd be more than willing to correct it.
      – Kusalananda
      Feb 17 at 18:13
















    • You worry about file name mangling but fail to get the replacement right. Matthew 7:3.
      – Gerard H. Pille
      Feb 17 at 17:38










    • @GerardH.Pille Please explain and I will get it right.
      – Kusalananda
      Feb 17 at 17:50










    • @GerardH.Pille If you referring to the string aux_power_w, then all I can say is that the question asks to replace with aux_power while his attempt is to replace with aux_power_w. The attempt don't match the question, and it's uncertain which one is actually meant. If I have another error in my code, I'd be more than willing to correct it.
      – Kusalananda
      Feb 17 at 18:13















    You worry about file name mangling but fail to get the replacement right. Matthew 7:3.
    – Gerard H. Pille
    Feb 17 at 17:38




    You worry about file name mangling but fail to get the replacement right. Matthew 7:3.
    – Gerard H. Pille
    Feb 17 at 17:38












    @GerardH.Pille Please explain and I will get it right.
    – Kusalananda
    Feb 17 at 17:50




    @GerardH.Pille Please explain and I will get it right.
    – Kusalananda
    Feb 17 at 17:50












    @GerardH.Pille If you referring to the string aux_power_w, then all I can say is that the question asks to replace with aux_power while his attempt is to replace with aux_power_w. The attempt don't match the question, and it's uncertain which one is actually meant. If I have another error in my code, I'd be more than willing to correct it.
    – Kusalananda
    Feb 17 at 18:13




    @GerardH.Pille If you referring to the string aux_power_w, then all I can say is that the question asks to replace with aux_power while his attempt is to replace with aux_power_w. The attempt don't match the question, and it's uncertain which one is actually meant. If I have another error in my code, I'd be more than willing to correct it.
    – Kusalananda
    Feb 17 at 18:13












    up vote
    1
    down vote













    You should escape the square brackets for grep. "[A]" is the same as "A".
    Try



    grep -lR "Aux Power [A]" /mnt/d/power/jan/output | xargs sed -i "s/Aux Power [A]/aux_power_w/g"





    share|improve this answer
























      up vote
      1
      down vote













      You should escape the square brackets for grep. "[A]" is the same as "A".
      Try



      grep -lR "Aux Power [A]" /mnt/d/power/jan/output | xargs sed -i "s/Aux Power [A]/aux_power_w/g"





      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        You should escape the square brackets for grep. "[A]" is the same as "A".
        Try



        grep -lR "Aux Power [A]" /mnt/d/power/jan/output | xargs sed -i "s/Aux Power [A]/aux_power_w/g"





        share|improve this answer












        You should escape the square brackets for grep. "[A]" is the same as "A".
        Try



        grep -lR "Aux Power [A]" /mnt/d/power/jan/output | xargs sed -i "s/Aux Power [A]/aux_power_w/g"






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 17 at 11:00









        Gerard H. Pille

        1,169212




        1,169212






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f424761%2freplace-string-with-spaces-in-entire-directory%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?