Replace each instance of a character in variable names across multiple files

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I have a list of template files that contain variables in this format:



my.var
my.other.var


I need to locate each instance and replace the . with _ so that each variable becomes:



my_var
my_other_var


I have tried doing this with sed and regex, but I am having trouble dealing with the fact that the variables can have any number of periods.



This is what I have so far:



for f in `cat list-of-files.txt`; do
sed -i .bak -E 's/(.*).(.*)/1_2/g' $f
done;


Which results in



my_var
my.other_var






share|improve this question





















  • Welcome to Unix Stackexchange! You can take the tour first and the learn How to Ask a good question. That makes it easier for us to help you.
    – andcoz
    Jul 18 at 14:06
















up vote
1
down vote

favorite












I have a list of template files that contain variables in this format:



my.var
my.other.var


I need to locate each instance and replace the . with _ so that each variable becomes:



my_var
my_other_var


I have tried doing this with sed and regex, but I am having trouble dealing with the fact that the variables can have any number of periods.



This is what I have so far:



for f in `cat list-of-files.txt`; do
sed -i .bak -E 's/(.*).(.*)/1_2/g' $f
done;


Which results in



my_var
my.other_var






share|improve this question





















  • Welcome to Unix Stackexchange! You can take the tour first and the learn How to Ask a good question. That makes it easier for us to help you.
    – andcoz
    Jul 18 at 14:06












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have a list of template files that contain variables in this format:



my.var
my.other.var


I need to locate each instance and replace the . with _ so that each variable becomes:



my_var
my_other_var


I have tried doing this with sed and regex, but I am having trouble dealing with the fact that the variables can have any number of periods.



This is what I have so far:



for f in `cat list-of-files.txt`; do
sed -i .bak -E 's/(.*).(.*)/1_2/g' $f
done;


Which results in



my_var
my.other_var






share|improve this question













I have a list of template files that contain variables in this format:



my.var
my.other.var


I need to locate each instance and replace the . with _ so that each variable becomes:



my_var
my_other_var


I have tried doing this with sed and regex, but I am having trouble dealing with the fact that the variables can have any number of periods.



This is what I have so far:



for f in `cat list-of-files.txt`; do
sed -i .bak -E 's/(.*).(.*)/1_2/g' $f
done;


Which results in



my_var
my.other_var








share|improve this question












share|improve this question




share|improve this question








edited Jul 18 at 14:05









andcoz

11.5k32938




11.5k32938









asked Jul 18 at 13:51









Fantic

184




184











  • Welcome to Unix Stackexchange! You can take the tour first and the learn How to Ask a good question. That makes it easier for us to help you.
    – andcoz
    Jul 18 at 14:06
















  • Welcome to Unix Stackexchange! You can take the tour first and the learn How to Ask a good question. That makes it easier for us to help you.
    – andcoz
    Jul 18 at 14:06















Welcome to Unix Stackexchange! You can take the tour first and the learn How to Ask a good question. That makes it easier for us to help you.
– andcoz
Jul 18 at 14:06




Welcome to Unix Stackexchange! You can take the tour first and the learn How to Ask a good question. That makes it easier for us to help you.
– andcoz
Jul 18 at 14:06










2 Answers
2






active

oldest

votes

















up vote
-1
down vote



accepted










Why not something like:



echo " my.other.var " | sed -n '/.*/s/./-/gp'


result:



 my-other-var 


Pattern match for the format first and then execute the required sed commands if that pattern is met i.e. substitute - for .






share|improve this answer





















  • Thanks! Additional note - this gave an error with the default version of sed on Mac OSX. I had to hop in to a Linux vm to use it.
    – Fantic
    Jul 18 at 14:25










  • The solution on Mac is to "brew install gnu-sed" and run the command with gsed instead of sed.
    – Fantic
    Jul 18 at 14:47











  • echo " my.other.var This will not work. It picks up false positives." | sed -n '/.*/s/./-/gp'
    – ctrl-alt-delor
    Jul 18 at 14:52

















up vote
1
down vote













I figured out a way to accomplish what I wanted with PERL as well. The trick was to use lazy modifiers.



perl -pi.back -e 's|(w+?).(w+?)|1_2|g' <filename>


my.var becomes my_var






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%2f456999%2freplace-each-instance-of-a-character-in-variable-names-across-multiple-files%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
    -1
    down vote



    accepted










    Why not something like:



    echo " my.other.var " | sed -n '/.*/s/./-/gp'


    result:



     my-other-var 


    Pattern match for the format first and then execute the required sed commands if that pattern is met i.e. substitute - for .






    share|improve this answer





















    • Thanks! Additional note - this gave an error with the default version of sed on Mac OSX. I had to hop in to a Linux vm to use it.
      – Fantic
      Jul 18 at 14:25










    • The solution on Mac is to "brew install gnu-sed" and run the command with gsed instead of sed.
      – Fantic
      Jul 18 at 14:47











    • echo " my.other.var This will not work. It picks up false positives." | sed -n '/.*/s/./-/gp'
      – ctrl-alt-delor
      Jul 18 at 14:52














    up vote
    -1
    down vote



    accepted










    Why not something like:



    echo " my.other.var " | sed -n '/.*/s/./-/gp'


    result:



     my-other-var 


    Pattern match for the format first and then execute the required sed commands if that pattern is met i.e. substitute - for .






    share|improve this answer





















    • Thanks! Additional note - this gave an error with the default version of sed on Mac OSX. I had to hop in to a Linux vm to use it.
      – Fantic
      Jul 18 at 14:25










    • The solution on Mac is to "brew install gnu-sed" and run the command with gsed instead of sed.
      – Fantic
      Jul 18 at 14:47











    • echo " my.other.var This will not work. It picks up false positives." | sed -n '/.*/s/./-/gp'
      – ctrl-alt-delor
      Jul 18 at 14:52












    up vote
    -1
    down vote



    accepted







    up vote
    -1
    down vote



    accepted






    Why not something like:



    echo " my.other.var " | sed -n '/.*/s/./-/gp'


    result:



     my-other-var 


    Pattern match for the format first and then execute the required sed commands if that pattern is met i.e. substitute - for .






    share|improve this answer













    Why not something like:



    echo " my.other.var " | sed -n '/.*/s/./-/gp'


    result:



     my-other-var 


    Pattern match for the format first and then execute the required sed commands if that pattern is met i.e. substitute - for .







    share|improve this answer













    share|improve this answer



    share|improve this answer











    answered Jul 18 at 14:11









    Raman Sailopal

    1,16317




    1,16317











    • Thanks! Additional note - this gave an error with the default version of sed on Mac OSX. I had to hop in to a Linux vm to use it.
      – Fantic
      Jul 18 at 14:25










    • The solution on Mac is to "brew install gnu-sed" and run the command with gsed instead of sed.
      – Fantic
      Jul 18 at 14:47











    • echo " my.other.var This will not work. It picks up false positives." | sed -n '/.*/s/./-/gp'
      – ctrl-alt-delor
      Jul 18 at 14:52
















    • Thanks! Additional note - this gave an error with the default version of sed on Mac OSX. I had to hop in to a Linux vm to use it.
      – Fantic
      Jul 18 at 14:25










    • The solution on Mac is to "brew install gnu-sed" and run the command with gsed instead of sed.
      – Fantic
      Jul 18 at 14:47











    • echo " my.other.var This will not work. It picks up false positives." | sed -n '/.*/s/./-/gp'
      – ctrl-alt-delor
      Jul 18 at 14:52















    Thanks! Additional note - this gave an error with the default version of sed on Mac OSX. I had to hop in to a Linux vm to use it.
    – Fantic
    Jul 18 at 14:25




    Thanks! Additional note - this gave an error with the default version of sed on Mac OSX. I had to hop in to a Linux vm to use it.
    – Fantic
    Jul 18 at 14:25












    The solution on Mac is to "brew install gnu-sed" and run the command with gsed instead of sed.
    – Fantic
    Jul 18 at 14:47





    The solution on Mac is to "brew install gnu-sed" and run the command with gsed instead of sed.
    – Fantic
    Jul 18 at 14:47













    echo " my.other.var This will not work. It picks up false positives." | sed -n '/.*/s/./-/gp'
    – ctrl-alt-delor
    Jul 18 at 14:52




    echo " my.other.var This will not work. It picks up false positives." | sed -n '/.*/s/./-/gp'
    – ctrl-alt-delor
    Jul 18 at 14:52












    up vote
    1
    down vote













    I figured out a way to accomplish what I wanted with PERL as well. The trick was to use lazy modifiers.



    perl -pi.back -e 's|(w+?).(w+?)|1_2|g' <filename>


    my.var becomes my_var






    share|improve this answer

























      up vote
      1
      down vote













      I figured out a way to accomplish what I wanted with PERL as well. The trick was to use lazy modifiers.



      perl -pi.back -e 's|(w+?).(w+?)|1_2|g' <filename>


      my.var becomes my_var






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        I figured out a way to accomplish what I wanted with PERL as well. The trick was to use lazy modifiers.



        perl -pi.back -e 's|(w+?).(w+?)|1_2|g' <filename>


        my.var becomes my_var






        share|improve this answer













        I figured out a way to accomplish what I wanted with PERL as well. The trick was to use lazy modifiers.



        perl -pi.back -e 's|(w+?).(w+?)|1_2|g' <filename>


        my.var becomes my_var







        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered Jul 18 at 14:37









        Fantic

        184




        184






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f456999%2freplace-each-instance-of-a-character-in-variable-names-across-multiple-files%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?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay