How to find the shortest number in a file [closed]












1















I'm trying to find the shortest number in the file but I do not know how to do it
This is my input file:



asd
qwmcqm
342
xsa
53425423432
54334
aseqwe
// so i want to print "342"


It has to be AWK script so i can't use "sort" (in shell), i've tried this:



while(( getline line < "FileInput" ) > 0 ){
if ( line ~ /[0-9]/ )
if (length(line) < L)
L = length(line)
}


but it doesn't work! It works only for largest number if i change "<" to ">" inside script.










share|improve this question















closed as unclear what you're asking by G-Man, Archemar, Christopher, Mr Shunz, X Tian Jan 25 at 10:24


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • 1





    Why do you need to use sort in the shell? Is that part of the assignment? awk has built-in sorting functions (asort and asorti).

    – jordanm
    Jan 23 at 23:46






  • 3





    You should define your terms. Is 1.00 a number? Which is “shorter”, 1.00 or 42? Is 0xC a number? Etc.  Please do not respond in comments; edit your question to make it clearer and more complete.

    – G-Man
    Jan 23 at 23:47






  • 1





    Also, do you just want to print 342? If so, a simple grep to sort to head will do.

    – Nasir Riley
    Jan 23 at 23:51






  • 2





    FYI using getline in this way is non-idiomatic - and unnecessary since awk reads and processes line-by-line (or, more formally, record-by-record) anyhow

    – steeldriver
    Jan 23 at 23:52
















1















I'm trying to find the shortest number in the file but I do not know how to do it
This is my input file:



asd
qwmcqm
342
xsa
53425423432
54334
aseqwe
// so i want to print "342"


It has to be AWK script so i can't use "sort" (in shell), i've tried this:



while(( getline line < "FileInput" ) > 0 ){
if ( line ~ /[0-9]/ )
if (length(line) < L)
L = length(line)
}


but it doesn't work! It works only for largest number if i change "<" to ">" inside script.










share|improve this question















closed as unclear what you're asking by G-Man, Archemar, Christopher, Mr Shunz, X Tian Jan 25 at 10:24


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • 1





    Why do you need to use sort in the shell? Is that part of the assignment? awk has built-in sorting functions (asort and asorti).

    – jordanm
    Jan 23 at 23:46






  • 3





    You should define your terms. Is 1.00 a number? Which is “shorter”, 1.00 or 42? Is 0xC a number? Etc.  Please do not respond in comments; edit your question to make it clearer and more complete.

    – G-Man
    Jan 23 at 23:47






  • 1





    Also, do you just want to print 342? If so, a simple grep to sort to head will do.

    – Nasir Riley
    Jan 23 at 23:51






  • 2





    FYI using getline in this way is non-idiomatic - and unnecessary since awk reads and processes line-by-line (or, more formally, record-by-record) anyhow

    – steeldriver
    Jan 23 at 23:52














1












1








1








I'm trying to find the shortest number in the file but I do not know how to do it
This is my input file:



asd
qwmcqm
342
xsa
53425423432
54334
aseqwe
// so i want to print "342"


It has to be AWK script so i can't use "sort" (in shell), i've tried this:



while(( getline line < "FileInput" ) > 0 ){
if ( line ~ /[0-9]/ )
if (length(line) < L)
L = length(line)
}


but it doesn't work! It works only for largest number if i change "<" to ">" inside script.










share|improve this question
















I'm trying to find the shortest number in the file but I do not know how to do it
This is my input file:



asd
qwmcqm
342
xsa
53425423432
54334
aseqwe
// so i want to print "342"


It has to be AWK script so i can't use "sort" (in shell), i've tried this:



while(( getline line < "FileInput" ) > 0 ){
if ( line ~ /[0-9]/ )
if (length(line) < L)
L = length(line)
}


but it doesn't work! It works only for largest number if i change "<" to ">" inside script.







awk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 23 at 23:36









jordanm

30.5k28694




30.5k28694










asked Jan 23 at 23:10









MikeMike

61




61




closed as unclear what you're asking by G-Man, Archemar, Christopher, Mr Shunz, X Tian Jan 25 at 10:24


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









closed as unclear what you're asking by G-Man, Archemar, Christopher, Mr Shunz, X Tian Jan 25 at 10:24


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 1





    Why do you need to use sort in the shell? Is that part of the assignment? awk has built-in sorting functions (asort and asorti).

    – jordanm
    Jan 23 at 23:46






  • 3





    You should define your terms. Is 1.00 a number? Which is “shorter”, 1.00 or 42? Is 0xC a number? Etc.  Please do not respond in comments; edit your question to make it clearer and more complete.

    – G-Man
    Jan 23 at 23:47






  • 1





    Also, do you just want to print 342? If so, a simple grep to sort to head will do.

    – Nasir Riley
    Jan 23 at 23:51






  • 2





    FYI using getline in this way is non-idiomatic - and unnecessary since awk reads and processes line-by-line (or, more formally, record-by-record) anyhow

    – steeldriver
    Jan 23 at 23:52














  • 1





    Why do you need to use sort in the shell? Is that part of the assignment? awk has built-in sorting functions (asort and asorti).

    – jordanm
    Jan 23 at 23:46






  • 3





    You should define your terms. Is 1.00 a number? Which is “shorter”, 1.00 or 42? Is 0xC a number? Etc.  Please do not respond in comments; edit your question to make it clearer and more complete.

    – G-Man
    Jan 23 at 23:47






  • 1





    Also, do you just want to print 342? If so, a simple grep to sort to head will do.

    – Nasir Riley
    Jan 23 at 23:51






  • 2





    FYI using getline in this way is non-idiomatic - and unnecessary since awk reads and processes line-by-line (or, more formally, record-by-record) anyhow

    – steeldriver
    Jan 23 at 23:52








1




1





Why do you need to use sort in the shell? Is that part of the assignment? awk has built-in sorting functions (asort and asorti).

– jordanm
Jan 23 at 23:46





Why do you need to use sort in the shell? Is that part of the assignment? awk has built-in sorting functions (asort and asorti).

– jordanm
Jan 23 at 23:46




3




3





You should define your terms. Is 1.00 a number? Which is “shorter”, 1.00 or 42? Is 0xC a number? Etc.  Please do not respond in comments; edit your question to make it clearer and more complete.

– G-Man
Jan 23 at 23:47





You should define your terms. Is 1.00 a number? Which is “shorter”, 1.00 or 42? Is 0xC a number? Etc.  Please do not respond in comments; edit your question to make it clearer and more complete.

– G-Man
Jan 23 at 23:47




1




1





Also, do you just want to print 342? If so, a simple grep to sort to head will do.

– Nasir Riley
Jan 23 at 23:51





Also, do you just want to print 342? If so, a simple grep to sort to head will do.

– Nasir Riley
Jan 23 at 23:51




2




2





FYI using getline in this way is non-idiomatic - and unnecessary since awk reads and processes line-by-line (or, more formally, record-by-record) anyhow

– steeldriver
Jan 23 at 23:52





FYI using getline in this way is non-idiomatic - and unnecessary since awk reads and processes line-by-line (or, more formally, record-by-record) anyhow

– steeldriver
Jan 23 at 23:52










1 Answer
1






active

oldest

votes


















3














The reason that your example works for finding the largest number and not the smallest number is because variables in awk which have not been explicitly set, default to 0. None of your matching lines will be less than 0.



Since you are wanting to compare with previously found numbers, you can ensure that the length of the first number found is set to L by including a check if L is 0 in your conditional (which will only be the case if it is the first number seen in the file).



if (length(line) < L || L == 0) {
L = length(line)
}





share|improve this answer



















  • 1





    Or to get the actual number as stated, using the default loop: awk '/^[0-9]+$/ && (length<length(X) || !length(X)) {X=$0} END{print X}' (Can add BEGIN{X=""} if desired but not needed; vars actually default to empty string which converts to zero.)

    – dave_thompson_085
    Jan 24 at 7:30


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














The reason that your example works for finding the largest number and not the smallest number is because variables in awk which have not been explicitly set, default to 0. None of your matching lines will be less than 0.



Since you are wanting to compare with previously found numbers, you can ensure that the length of the first number found is set to L by including a check if L is 0 in your conditional (which will only be the case if it is the first number seen in the file).



if (length(line) < L || L == 0) {
L = length(line)
}





share|improve this answer



















  • 1





    Or to get the actual number as stated, using the default loop: awk '/^[0-9]+$/ && (length<length(X) || !length(X)) {X=$0} END{print X}' (Can add BEGIN{X=""} if desired but not needed; vars actually default to empty string which converts to zero.)

    – dave_thompson_085
    Jan 24 at 7:30
















3














The reason that your example works for finding the largest number and not the smallest number is because variables in awk which have not been explicitly set, default to 0. None of your matching lines will be less than 0.



Since you are wanting to compare with previously found numbers, you can ensure that the length of the first number found is set to L by including a check if L is 0 in your conditional (which will only be the case if it is the first number seen in the file).



if (length(line) < L || L == 0) {
L = length(line)
}





share|improve this answer



















  • 1





    Or to get the actual number as stated, using the default loop: awk '/^[0-9]+$/ && (length<length(X) || !length(X)) {X=$0} END{print X}' (Can add BEGIN{X=""} if desired but not needed; vars actually default to empty string which converts to zero.)

    – dave_thompson_085
    Jan 24 at 7:30














3












3








3







The reason that your example works for finding the largest number and not the smallest number is because variables in awk which have not been explicitly set, default to 0. None of your matching lines will be less than 0.



Since you are wanting to compare with previously found numbers, you can ensure that the length of the first number found is set to L by including a check if L is 0 in your conditional (which will only be the case if it is the first number seen in the file).



if (length(line) < L || L == 0) {
L = length(line)
}





share|improve this answer













The reason that your example works for finding the largest number and not the smallest number is because variables in awk which have not been explicitly set, default to 0. None of your matching lines will be less than 0.



Since you are wanting to compare with previously found numbers, you can ensure that the length of the first number found is set to L by including a check if L is 0 in your conditional (which will only be the case if it is the first number seen in the file).



if (length(line) < L || L == 0) {
L = length(line)
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 23 at 23:44









jordanmjordanm

30.5k28694




30.5k28694








  • 1





    Or to get the actual number as stated, using the default loop: awk '/^[0-9]+$/ && (length<length(X) || !length(X)) {X=$0} END{print X}' (Can add BEGIN{X=""} if desired but not needed; vars actually default to empty string which converts to zero.)

    – dave_thompson_085
    Jan 24 at 7:30














  • 1





    Or to get the actual number as stated, using the default loop: awk '/^[0-9]+$/ && (length<length(X) || !length(X)) {X=$0} END{print X}' (Can add BEGIN{X=""} if desired but not needed; vars actually default to empty string which converts to zero.)

    – dave_thompson_085
    Jan 24 at 7:30








1




1





Or to get the actual number as stated, using the default loop: awk '/^[0-9]+$/ && (length<length(X) || !length(X)) {X=$0} END{print X}' (Can add BEGIN{X=""} if desired but not needed; vars actually default to empty string which converts to zero.)

– dave_thompson_085
Jan 24 at 7:30





Or to get the actual number as stated, using the default loop: awk '/^[0-9]+$/ && (length<length(X) || !length(X)) {X=$0} END{print X}' (Can add BEGIN{X=""} if desired but not needed; vars actually default to empty string which converts to zero.)

– dave_thompson_085
Jan 24 at 7:30



Popular posts from this blog

How to reconfigure Docker Trusted Registry 2.x.x to use CEPH FS mount instead of NFS and other traditional...

is 'sed' thread safe

How to make a Squid Proxy server?