How to replace a number by its corresponding string (low/high) in bash?

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











up vote
0
down vote

favorite












I have a file:



property(Address1, 4.5)
property(Address2, 2.2)
property(Address3, 9.0)
property(Address4, 3.4)
---


I want to replace the floating points based on some conditions (For example: if the number is between 2 and 4, I will replace it by Low, otherwise High) to produce this:



property(Address1, High)
property(Address2, Low)
property(Address3, High)
property(Address4, Low)
---


My try: (1) First extract the number using $. (2) Check whether the condition satisfies (3) then print it. But in this process I am failing to output the Address* to the output file.










share|improve this question























  • The process you describe sounds correct. After you calculate whether the floating point is high or low, you can use something like sed to replace the number in the string with high or low.
    – Peschke
    Dec 1 at 5:22














up vote
0
down vote

favorite












I have a file:



property(Address1, 4.5)
property(Address2, 2.2)
property(Address3, 9.0)
property(Address4, 3.4)
---


I want to replace the floating points based on some conditions (For example: if the number is between 2 and 4, I will replace it by Low, otherwise High) to produce this:



property(Address1, High)
property(Address2, Low)
property(Address3, High)
property(Address4, Low)
---


My try: (1) First extract the number using $. (2) Check whether the condition satisfies (3) then print it. But in this process I am failing to output the Address* to the output file.










share|improve this question























  • The process you describe sounds correct. After you calculate whether the floating point is high or low, you can use something like sed to replace the number in the string with high or low.
    – Peschke
    Dec 1 at 5:22












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a file:



property(Address1, 4.5)
property(Address2, 2.2)
property(Address3, 9.0)
property(Address4, 3.4)
---


I want to replace the floating points based on some conditions (For example: if the number is between 2 and 4, I will replace it by Low, otherwise High) to produce this:



property(Address1, High)
property(Address2, Low)
property(Address3, High)
property(Address4, Low)
---


My try: (1) First extract the number using $. (2) Check whether the condition satisfies (3) then print it. But in this process I am failing to output the Address* to the output file.










share|improve this question















I have a file:



property(Address1, 4.5)
property(Address2, 2.2)
property(Address3, 9.0)
property(Address4, 3.4)
---


I want to replace the floating points based on some conditions (For example: if the number is between 2 and 4, I will replace it by Low, otherwise High) to produce this:



property(Address1, High)
property(Address2, Low)
property(Address3, High)
property(Address4, Low)
---


My try: (1) First extract the number using $. (2) Check whether the condition satisfies (3) then print it. But in this process I am failing to output the Address* to the output file.







shell-script shell awk replace






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 1 at 6:07

























asked Dec 1 at 5:17









Coder

1126




1126











  • The process you describe sounds correct. After you calculate whether the floating point is high or low, you can use something like sed to replace the number in the string with high or low.
    – Peschke
    Dec 1 at 5:22
















  • The process you describe sounds correct. After you calculate whether the floating point is high or low, you can use something like sed to replace the number in the string with high or low.
    – Peschke
    Dec 1 at 5:22















The process you describe sounds correct. After you calculate whether the floating point is high or low, you can use something like sed to replace the number in the string with high or low.
– Peschke
Dec 1 at 5:22




The process you describe sounds correct. After you calculate whether the floating point is high or low, you can use something like sed to replace the number in the string with high or low.
– Peschke
Dec 1 at 5:22










3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










This does (strictly) what you ask for:



awk ' print $1, (($2>2)&&($2<4))?"Low)":"High)" '


But it is taking advantage of the fact that an string like 9.0) (with the trailing parenthesis) is converted to the number 9 by awk. That may fail if an space gets added for example.



A more strict solution is to also use the closing parenthesis as a field delimiter to remove it from the contents of field number 2.



awk -F'[ )]' 'print $1,(($2>2)&&($2<4))?"Low)":"High)"'





share|improve this answer





























    up vote
    1
    down vote













    I have tried with if and else condition



    sed "s/)$//g" filename| awk 'if($2 > 2 && $2 < 4) $2="low)";print $0else$2="high)";print $0'
    property(Address1, high)
    property(Address2, low)
    property(Address3, high)
    property(Address4, low)





    share|improve this answer



























      up vote
      1
      down vote













      With perl, assuming numbers are in the format [-]<digits>[.<digits>], and matching on them wherever they are in the input provided they're neither preceded nor followed by word characters (so Address1 is not changed to AddressHigh for instance):



      <input perl -pe 's(?<!w)-?d[d.]*(?!w)$& >=2 && $& <=4 ? "Low" : "High"ge'





      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: 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%2f485299%2fhow-to-replace-a-number-by-its-corresponding-string-low-high-in-bash%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        2
        down vote



        accepted










        This does (strictly) what you ask for:



        awk ' print $1, (($2>2)&&($2<4))?"Low)":"High)" '


        But it is taking advantage of the fact that an string like 9.0) (with the trailing parenthesis) is converted to the number 9 by awk. That may fail if an space gets added for example.



        A more strict solution is to also use the closing parenthesis as a field delimiter to remove it from the contents of field number 2.



        awk -F'[ )]' 'print $1,(($2>2)&&($2<4))?"Low)":"High)"'





        share|improve this answer


























          up vote
          2
          down vote



          accepted










          This does (strictly) what you ask for:



          awk ' print $1, (($2>2)&&($2<4))?"Low)":"High)" '


          But it is taking advantage of the fact that an string like 9.0) (with the trailing parenthesis) is converted to the number 9 by awk. That may fail if an space gets added for example.



          A more strict solution is to also use the closing parenthesis as a field delimiter to remove it from the contents of field number 2.



          awk -F'[ )]' 'print $1,(($2>2)&&($2<4))?"Low)":"High)"'





          share|improve this answer
























            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            This does (strictly) what you ask for:



            awk ' print $1, (($2>2)&&($2<4))?"Low)":"High)" '


            But it is taking advantage of the fact that an string like 9.0) (with the trailing parenthesis) is converted to the number 9 by awk. That may fail if an space gets added for example.



            A more strict solution is to also use the closing parenthesis as a field delimiter to remove it from the contents of field number 2.



            awk -F'[ )]' 'print $1,(($2>2)&&($2<4))?"Low)":"High)"'





            share|improve this answer














            This does (strictly) what you ask for:



            awk ' print $1, (($2>2)&&($2<4))?"Low)":"High)" '


            But it is taking advantage of the fact that an string like 9.0) (with the trailing parenthesis) is converted to the number 9 by awk. That may fail if an space gets added for example.



            A more strict solution is to also use the closing parenthesis as a field delimiter to remove it from the contents of field number 2.



            awk -F'[ )]' 'print $1,(($2>2)&&($2<4))?"Low)":"High)"'






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 1 at 7:03

























            answered Dec 1 at 5:58









            Isaac

            10.8k11447




            10.8k11447






















                up vote
                1
                down vote













                I have tried with if and else condition



                sed "s/)$//g" filename| awk 'if($2 > 2 && $2 < 4) $2="low)";print $0else$2="high)";print $0'
                property(Address1, high)
                property(Address2, low)
                property(Address3, high)
                property(Address4, low)





                share|improve this answer
























                  up vote
                  1
                  down vote













                  I have tried with if and else condition



                  sed "s/)$//g" filename| awk 'if($2 > 2 && $2 < 4) $2="low)";print $0else$2="high)";print $0'
                  property(Address1, high)
                  property(Address2, low)
                  property(Address3, high)
                  property(Address4, low)





                  share|improve this answer






















                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    I have tried with if and else condition



                    sed "s/)$//g" filename| awk 'if($2 > 2 && $2 < 4) $2="low)";print $0else$2="high)";print $0'
                    property(Address1, high)
                    property(Address2, low)
                    property(Address3, high)
                    property(Address4, low)





                    share|improve this answer












                    I have tried with if and else condition



                    sed "s/)$//g" filename| awk 'if($2 > 2 && $2 < 4) $2="low)";print $0else$2="high)";print $0'
                    property(Address1, high)
                    property(Address2, low)
                    property(Address3, high)
                    property(Address4, low)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 1 at 6:21









                    Praveen Kumar BS

                    1,162138




                    1,162138




















                        up vote
                        1
                        down vote













                        With perl, assuming numbers are in the format [-]<digits>[.<digits>], and matching on them wherever they are in the input provided they're neither preceded nor followed by word characters (so Address1 is not changed to AddressHigh for instance):



                        <input perl -pe 's(?<!w)-?d[d.]*(?!w)$& >=2 && $& <=4 ? "Low" : "High"ge'





                        share|improve this answer


























                          up vote
                          1
                          down vote













                          With perl, assuming numbers are in the format [-]<digits>[.<digits>], and matching on them wherever they are in the input provided they're neither preceded nor followed by word characters (so Address1 is not changed to AddressHigh for instance):



                          <input perl -pe 's(?<!w)-?d[d.]*(?!w)$& >=2 && $& <=4 ? "Low" : "High"ge'





                          share|improve this answer
























                            up vote
                            1
                            down vote










                            up vote
                            1
                            down vote









                            With perl, assuming numbers are in the format [-]<digits>[.<digits>], and matching on them wherever they are in the input provided they're neither preceded nor followed by word characters (so Address1 is not changed to AddressHigh for instance):



                            <input perl -pe 's(?<!w)-?d[d.]*(?!w)$& >=2 && $& <=4 ? "Low" : "High"ge'





                            share|improve this answer














                            With perl, assuming numbers are in the format [-]<digits>[.<digits>], and matching on them wherever they are in the input provided they're neither preceded nor followed by word characters (so Address1 is not changed to AddressHigh for instance):



                            <input perl -pe 's(?<!w)-?d[d.]*(?!w)$& >=2 && $& <=4 ? "Low" : "High"ge'






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Dec 1 at 10:28

























                            answered Dec 1 at 7:25









                            Stéphane Chazelas

                            296k54560905




                            296k54560905



























                                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.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • 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%2f485299%2fhow-to-replace-a-number-by-its-corresponding-string-low-high-in-bash%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 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?