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.
awk sed regular-expression numeric-data
New contributor
add a comment |
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.
awk sed regular-expression numeric-data
New contributor
Regex is probably not the best way to approach this. As long as you're dealing only with whole numbers (i.e., no17.42
), you can do the mathematical comparison directly in the shell.
– Scott
Nov 21 at 17:26
add a comment |
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.
awk sed regular-expression numeric-data
New contributor
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
awk sed regular-expression numeric-data
New contributor
New contributor
edited Nov 21 at 18:57
Rui F Ribeiro
38.3k1475126
38.3k1475126
New contributor
asked Nov 21 at 17:19
Selim Turkoglu
153
153
New contributor
New contributor
Regex is probably not the best way to approach this. As long as you're dealing only with whole numbers (i.e., no17.42
), you can do the mathematical comparison directly in the shell.
– Scott
Nov 21 at 17:26
add a comment |
Regex is probably not the best way to approach this. As long as you're dealing only with whole numbers (i.e., no17.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
add a comment |
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?
1
after if condition I added exit 1 command. It worked
– Selim Turkoglu
Nov 21 at 17:45
add a comment |
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.
add a comment |
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
'
add a comment |
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.
add a comment |
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?
1
after if condition I added exit 1 command. It worked
– Selim Turkoglu
Nov 21 at 17:45
add a comment |
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?
1
after if condition I added exit 1 command. It worked
– Selim Turkoglu
Nov 21 at 17:45
add a comment |
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?
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?
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 21 at 17:39
Jeff Schaller
36.6k1052121
36.6k1052121
add a comment |
add a comment |
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
'
add a comment |
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
'
add a comment |
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
'
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
'
answered Nov 21 at 22:13
ctac_
1,344117
1,344117
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 21 at 17:50
djs
1012
1012
add a comment |
add a comment |
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.
Selim Turkoglu is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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