Compare the numbers in text file, if meets the condition run a shell script











up vote
2
down vote

favorite












I have a text file (created by a script) which contains only numbers in a single line like "5 17 42 2 87 33". I want to check every number with 50 (example), and if any of those numbers is greater than 50, I want to run another shell script.
I'm using a vumeter program, my purpose is to run a sound recognition program if noise level is high. So I just want to determine a threshold.










share|improve this question









New contributor




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




















  • Regex is probably not the best way to approach this. As long as you're dealing only with whole numbers (i.e., no 17.42), you can do the mathematical comparison directly in the shell.
    – Scott
    Nov 21 at 17:26















up vote
2
down vote

favorite












I have a text file (created by a script) which contains only numbers in a single line like "5 17 42 2 87 33". I want to check every number with 50 (example), and if any of those numbers is greater than 50, I want to run another shell script.
I'm using a vumeter program, my purpose is to run a sound recognition program if noise level is high. So I just want to determine a threshold.










share|improve this question









New contributor




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




















  • Regex is probably not the best way to approach this. As long as you're dealing only with whole numbers (i.e., no 17.42), you can do the mathematical comparison directly in the shell.
    – Scott
    Nov 21 at 17:26













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have a text file (created by a script) which contains only numbers in a single line like "5 17 42 2 87 33". I want to check every number with 50 (example), and if any of those numbers is greater than 50, I want to run another shell script.
I'm using a vumeter program, my purpose is to run a sound recognition program if noise level is high. So I just want to determine a threshold.










share|improve this question









New contributor




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











I have a text file (created by a script) which contains only numbers in a single line like "5 17 42 2 87 33". I want to check every number with 50 (example), and if any of those numbers is greater than 50, I want to run another shell script.
I'm using a vumeter program, my purpose is to run a sound recognition program if noise level is high. So I just want to determine a threshold.







awk sed regular-expression numeric-data






share|improve this question









New contributor




Selim Turkoglu 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




Selim Turkoglu 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 Nov 21 at 18:57









Rui F Ribeiro

38.3k1475126




38.3k1475126






New contributor




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









asked Nov 21 at 17:19









Selim Turkoglu

153




153




New contributor




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





New contributor





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






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












  • Regex is probably not the best way to approach this. As long as you're dealing only with whole numbers (i.e., no 17.42), you can do the mathematical comparison directly in the shell.
    – Scott
    Nov 21 at 17:26


















  • Regex is probably not the best way to approach this. As long as you're dealing only with whole numbers (i.e., no 17.42), you can do the mathematical comparison directly in the shell.
    – Scott
    Nov 21 at 17:26
















Regex is probably not the best way to approach this. As long as you're dealing only with whole numbers (i.e., no 17.42), you can do the mathematical comparison directly in the shell.
– Scott
Nov 21 at 17:26




Regex is probably not the best way to approach this. As long as you're dealing only with whole numbers (i.e., no 17.42), you can do the mathematical comparison directly in the shell.
– Scott
Nov 21 at 17:26










4 Answers
4






active

oldest

votes

















up vote
0
down vote



accepted










Using awk:



awk '{ for(i = 1; i <= NF; i++) if($i > 50) { system("bash /other/shell/script.sh") } }' file.txt


Using a bash script:



#!/bin/bash

file=/path/to/file.txt

for num in $(<"$file"); do
if ((num>50)); then
bash /other/shell/script.sh
fi
done


This will loop through each number in file.txt and check if that number is greater than 50, then run the script you provide. However this may be an issue or unnecessary; If there are multiple numbers in your text file greater than 50 should you run your script multiple times?






share|improve this answer



















  • 1




    after if condition I added exit 1 command. It worked
    – Selim Turkoglu
    Nov 21 at 17:45


















up vote
1
down vote













As a parameter-accepting function and input as the hard-coded filename:



greaterthan() (
threshold=$1
set -- $(< input)
for arg
do
if [ "$arg" -gt "$threshold" ]
then
echo execute other shell script
break
fi
done
)


Source that in, or make it a script, and call it like greaterthan 50 or whatever number you like.






share|improve this answer




























    up vote
    1
    down vote













    You can use dc :



    dc -f lefile -e '
    [ c # clear the stack
    ! xmessage "a number is greater than 50" & # pop up a message
    ] sr
    [ 50 <r # if a number > 50 execute macro r
    z 0 <t # if stack not empty execute macro t
    ] st
    lt x
    '





    share|improve this answer




























      up vote
      0
      down vote













      You could split the file into lines, then search for lines matching numbers greater than or equal to 50:



      fmt -w1 | egrep -q '(ddd|[5-9]d)(..*)?' && sh other_script.sh



      The first parenthesized clause in regexp looks for at least three digits, or two digits starting with a '5'. The second clause allows an option decimal point and any suffix.



      Use the -q option to suppress normal output, because we only want the exit status.






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


        }
        });






        Selim Turkoglu 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%2f483262%2fcompare-the-numbers-in-text-file-if-meets-the-condition-run-a-shell-script%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        0
        down vote



        accepted










        Using awk:



        awk '{ for(i = 1; i <= NF; i++) if($i > 50) { system("bash /other/shell/script.sh") } }' file.txt


        Using a bash script:



        #!/bin/bash

        file=/path/to/file.txt

        for num in $(<"$file"); do
        if ((num>50)); then
        bash /other/shell/script.sh
        fi
        done


        This will loop through each number in file.txt and check if that number is greater than 50, then run the script you provide. However this may be an issue or unnecessary; If there are multiple numbers in your text file greater than 50 should you run your script multiple times?






        share|improve this answer



















        • 1




          after if condition I added exit 1 command. It worked
          – Selim Turkoglu
          Nov 21 at 17:45















        up vote
        0
        down vote



        accepted










        Using awk:



        awk '{ for(i = 1; i <= NF; i++) if($i > 50) { system("bash /other/shell/script.sh") } }' file.txt


        Using a bash script:



        #!/bin/bash

        file=/path/to/file.txt

        for num in $(<"$file"); do
        if ((num>50)); then
        bash /other/shell/script.sh
        fi
        done


        This will loop through each number in file.txt and check if that number is greater than 50, then run the script you provide. However this may be an issue or unnecessary; If there are multiple numbers in your text file greater than 50 should you run your script multiple times?






        share|improve this answer



















        • 1




          after if condition I added exit 1 command. It worked
          – Selim Turkoglu
          Nov 21 at 17:45













        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        Using awk:



        awk '{ for(i = 1; i <= NF; i++) if($i > 50) { system("bash /other/shell/script.sh") } }' file.txt


        Using a bash script:



        #!/bin/bash

        file=/path/to/file.txt

        for num in $(<"$file"); do
        if ((num>50)); then
        bash /other/shell/script.sh
        fi
        done


        This will loop through each number in file.txt and check if that number is greater than 50, then run the script you provide. However this may be an issue or unnecessary; If there are multiple numbers in your text file greater than 50 should you run your script multiple times?






        share|improve this answer














        Using awk:



        awk '{ for(i = 1; i <= NF; i++) if($i > 50) { system("bash /other/shell/script.sh") } }' file.txt


        Using a bash script:



        #!/bin/bash

        file=/path/to/file.txt

        for num in $(<"$file"); do
        if ((num>50)); then
        bash /other/shell/script.sh
        fi
        done


        This will loop through each number in file.txt and check if that number is greater than 50, then run the script you provide. However this may be an issue or unnecessary; If there are multiple numbers in your text file greater than 50 should you run your script multiple times?







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 at 17:36

























        answered Nov 21 at 17:26









        Jesse_b

        11.4k23063




        11.4k23063








        • 1




          after if condition I added exit 1 command. It worked
          – Selim Turkoglu
          Nov 21 at 17:45














        • 1




          after if condition I added exit 1 command. It worked
          – Selim Turkoglu
          Nov 21 at 17:45








        1




        1




        after if condition I added exit 1 command. It worked
        – Selim Turkoglu
        Nov 21 at 17:45




        after if condition I added exit 1 command. It worked
        – Selim Turkoglu
        Nov 21 at 17:45












        up vote
        1
        down vote













        As a parameter-accepting function and input as the hard-coded filename:



        greaterthan() (
        threshold=$1
        set -- $(< input)
        for arg
        do
        if [ "$arg" -gt "$threshold" ]
        then
        echo execute other shell script
        break
        fi
        done
        )


        Source that in, or make it a script, and call it like greaterthan 50 or whatever number you like.






        share|improve this answer

























          up vote
          1
          down vote













          As a parameter-accepting function and input as the hard-coded filename:



          greaterthan() (
          threshold=$1
          set -- $(< input)
          for arg
          do
          if [ "$arg" -gt "$threshold" ]
          then
          echo execute other shell script
          break
          fi
          done
          )


          Source that in, or make it a script, and call it like greaterthan 50 or whatever number you like.






          share|improve this answer























            up vote
            1
            down vote










            up vote
            1
            down vote









            As a parameter-accepting function and input as the hard-coded filename:



            greaterthan() (
            threshold=$1
            set -- $(< input)
            for arg
            do
            if [ "$arg" -gt "$threshold" ]
            then
            echo execute other shell script
            break
            fi
            done
            )


            Source that in, or make it a script, and call it like greaterthan 50 or whatever number you like.






            share|improve this answer












            As a parameter-accepting function and input as the hard-coded filename:



            greaterthan() (
            threshold=$1
            set -- $(< input)
            for arg
            do
            if [ "$arg" -gt "$threshold" ]
            then
            echo execute other shell script
            break
            fi
            done
            )


            Source that in, or make it a script, and call it like greaterthan 50 or whatever number you like.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 at 17:39









            Jeff Schaller

            36.6k1052121




            36.6k1052121






















                up vote
                1
                down vote













                You can use dc :



                dc -f lefile -e '
                [ c # clear the stack
                ! xmessage "a number is greater than 50" & # pop up a message
                ] sr
                [ 50 <r # if a number > 50 execute macro r
                z 0 <t # if stack not empty execute macro t
                ] st
                lt x
                '





                share|improve this answer

























                  up vote
                  1
                  down vote













                  You can use dc :



                  dc -f lefile -e '
                  [ c # clear the stack
                  ! xmessage "a number is greater than 50" & # pop up a message
                  ] sr
                  [ 50 <r # if a number > 50 execute macro r
                  z 0 <t # if stack not empty execute macro t
                  ] st
                  lt x
                  '





                  share|improve this answer























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    You can use dc :



                    dc -f lefile -e '
                    [ c # clear the stack
                    ! xmessage "a number is greater than 50" & # pop up a message
                    ] sr
                    [ 50 <r # if a number > 50 execute macro r
                    z 0 <t # if stack not empty execute macro t
                    ] st
                    lt x
                    '





                    share|improve this answer












                    You can use dc :



                    dc -f lefile -e '
                    [ c # clear the stack
                    ! xmessage "a number is greater than 50" & # pop up a message
                    ] sr
                    [ 50 <r # if a number > 50 execute macro r
                    z 0 <t # if stack not empty execute macro t
                    ] st
                    lt x
                    '






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 21 at 22:13









                    ctac_

                    1,344117




                    1,344117






















                        up vote
                        0
                        down vote













                        You could split the file into lines, then search for lines matching numbers greater than or equal to 50:



                        fmt -w1 | egrep -q '(ddd|[5-9]d)(..*)?' && sh other_script.sh



                        The first parenthesized clause in regexp looks for at least three digits, or two digits starting with a '5'. The second clause allows an option decimal point and any suffix.



                        Use the -q option to suppress normal output, because we only want the exit status.






                        share|improve this answer

























                          up vote
                          0
                          down vote













                          You could split the file into lines, then search for lines matching numbers greater than or equal to 50:



                          fmt -w1 | egrep -q '(ddd|[5-9]d)(..*)?' && sh other_script.sh



                          The first parenthesized clause in regexp looks for at least three digits, or two digits starting with a '5'. The second clause allows an option decimal point and any suffix.



                          Use the -q option to suppress normal output, because we only want the exit status.






                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            You could split the file into lines, then search for lines matching numbers greater than or equal to 50:



                            fmt -w1 | egrep -q '(ddd|[5-9]d)(..*)?' && sh other_script.sh



                            The first parenthesized clause in regexp looks for at least three digits, or two digits starting with a '5'. The second clause allows an option decimal point and any suffix.



                            Use the -q option to suppress normal output, because we only want the exit status.






                            share|improve this answer












                            You could split the file into lines, then search for lines matching numbers greater than or equal to 50:



                            fmt -w1 | egrep -q '(ddd|[5-9]d)(..*)?' && sh other_script.sh



                            The first parenthesized clause in regexp looks for at least three digits, or two digits starting with a '5'. The second clause allows an option decimal point and any suffix.



                            Use the -q option to suppress normal output, because we only want the exit status.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 21 at 17:50









                            djs

                            1012




                            1012






















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










                                 

                                draft saved


                                draft discarded


















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













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












                                Selim Turkoglu 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%2f483262%2fcompare-the-numbers-in-text-file-if-meets-the-condition-run-a-shell-script%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

                                Morgemoulin

                                Scott Moir

                                Souastre