Print smallest integer from file using awk custom function?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print smallest integer from file using awk custom function?
# 1  
Old 01-08-2016
Print smallest integer from file using awk custom function?

`awk` function looks like this in a file name `fun.awk`:
Code:
    {
    print small()
    }
    function small()
    
    {
    a[NR]=$0
    
    smal=0
    
    for(i=1;i<=3;i++)
    {
    if( a[i]<a[i+1])
    
    smal=a[i]
    
    else
    
    smal=a[i+1]
   
    }
    return smal
    }

The contents of `awk.write`:

Quote:
1
23
32
The `awk` command is:

awk -f fun.awk awk.write

It gives me no result? Why?
# 2  
Old 01-08-2016
Hello lazerz,

Could you please try following and let me know if this helps you. Let's say we have following Input_file.
Code:
 
1
23
32
67
78
89
100
234
12
0
-1
12
233
13
15
13

Then following code may help us to achieve as your request.
Code:
awk 'NF{A[NR]=$0;n=NR} END{for(i=1;i<=n;i++){Y=Y<A[i]?Y:A[i]};print Y}'  Input_file

Output will be as follows.
Code:
 -1

Thanks,
R. Singh

Last edited by RavinderSingh13; 01-08-2016 at 10:47 AM.. Reason: Added an example file and output to my post now.
# 3  
Old 01-08-2016
Quote:
Originally Posted by RavinderSingh13
Hello lazerz,

Could you please try following and let me know if this helps you.
Code:
awk 'NF{A[NR]=$0;n=NR} END{for(i=1;i<=n;i++){Y=Y<A[i]?Y:A[i]};print Y}'  Input_file

Thanks,
R. Singh
Thxs, i can do with one-liner I need to do in function.

---------- Post updated at 10:00 AM ---------- Previous update was at 09:46 AM ----------

Quote:
Originally Posted by RavinderSingh13
Hello lazerz,

Could you please try following and let me know if this helps you. Let's say we have following Input_file.
Code:
 
1
23
32
67
78
89
100
234
12
0
-1
12
233
13
15
13

Then following code may help us to achieve as your request.
Code:
awk 'NF{A[NR]=$0;n=NR} END{for(i=1;i<=n;i++){Y=Y<A[i]?Y:A[i]};print Y}'  Input_file

Output will be as follows.
Code:
 -1

Thanks,
R. Singh
can this same be translate into function?
# 4  
Old 01-09-2016
Quote:
Originally Posted by lazerz
`awk` function looks like this in a file name `fun.awk`:
Code:
    {
    print small()
    }
    function small()
    
    {
    a[NR]=$0
    
    smal=0
    
    for(i=1;i<=3;i++)
    {
    if( a[i]<a[i+1])
    
    smal=a[i]
    
    else
    
    smal=a[i+1]
   
    }
    return smal
    }

The contents of `awk.write`:



The `awk` command is:

awk -f fun.awk awk.write

It gives me no result? Why?
Saying the above script gives you no result is misleading. With a three line input file, it produces three lines of output (each output line being an empty line). You haven't said what you are trying to do with this script (or function). As written, it appears that the intent is to print the smallest value read so far for each line read. I.e., the output from the 1st line read will be the first line read, the output from the 2nd line read will be the smaller of the 1st two values read, and the output from the 3rd line read will be the smallest of the 1st three values read.

But, your function doesn't look for the smallest value in the number of lines read; instead it looks for the smaller value in the 3rd and 4th values (in an array that never has more than three values included when reading a three line input file). And, since the 4th value in that array is the default empty string assumed by any unassigned variable (which is treated as a zero value when compared to an integer), the result of each call to your function was an empty string.

Rewriting fun.awk to only use elements of the array that have actually been assigned variables (and reformatting to make the structure more obvious):
Code:
{
    print small()
}
function small() {
    smal=a[NR]=$0
    for(i = 1; i < NR; i++) {
	if(a[i] < smal)
	    smal=a[i]
    }
    return smal
}

makes the command line:
Code:
awk -f fun.awk awk.write

produce the output:
Code:
1
1
1

And if the data sample suggested in post #2 is stored in a file named file, the command:
Code:
awk -f fun.awk file

produces the output:
Code:
1
1
1
1
1
1
1
1
1
0
-1
-1
-1
-1
-1
-1

If the goal of the script is to just print the smallest value read (and a function is required rather than keeping track of the smallest value seen while reading the data), that would be written as:
Code:
{
    small()
}
function small() {
    smal=a[NR]=$0
    for(i = 1; i < NR; i++) {
	if(a[i] < smal)
	    smal=a[i]
    }
    return smal
}
END {
    print small()
}

or, MUCH more efficiently, as:
Code:
{
    a[NR] = $0
}
function small() {
    smal = a[NR]
    for(i = 1; i < NR; i++) {
	if(a[i] < smal)
	    smal=a[i]
    }
    return smal
}
END {
    print small()
}

And, of course, if you don't insist on a function, a better way to do it if you want to use awk would be:
Code:
NR == 1 {
	small = $0
	next
}
$0 < small {
	small = $0
}
END {	print small
}

This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 01-12-2016
Thank you so much Don you explained the code like a good and sincere teacher. I have nothing more to say.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script to print the smallest floating point number in a row that is not 0

Hello, I have often found bash to be difficult when it comes to floating point numbers. I have data with rows of tab delimited floating point numbers. I need to find the smallest number in each row that is not 0.0. Numbers can be negative and they do not come in any particular order for a given... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

2. Shell Programming and Scripting

Problem facing to compare different column and print out record with smallest number

Hi, Input file 1 : 37170 37196 77 51 37174 37195 73 52 37174 37194 73 53 Desired Output file 1 : 37170 37196 77 51 Input file 2 : 37174 37195 73 0 37170 37196 77 0 Desired Output file 2 : 37174 37195 73 0 (1 Reply)
Discussion started by: cpp_beginner
1 Replies

3. Shell Programming and Scripting

Problem to print out record got smallest number in specific column

Hi, Anybody know how to print out the record that shown smallest number among column 3 and column 4 Case 1 Input : 37170 37196 77 51 37174 37195 73 52 37174 37194 73 53 Case 1 Output : 37170 37196 77 51 Case 2 Input : 469613 469660 73 ... (4 Replies)
Discussion started by: cpp_beginner
4 Replies

4. Shell Programming and Scripting

Print smallest negative number with corresponding index from a column

considering the following table: ID col1 col2 col3 col4 1 -16.06801249 13.49785832 -56.57087607 -27.00500526 2 -1.53315720 0.71731735 -42.03602078 -39.78554623 3 -1.53315190 0.71731587 -42.03601548 ... (3 Replies)
Discussion started by: Birda
3 Replies

5. Shell Programming and Scripting

AWK (how) to get smallest/largest nr of ls -la

Hey, This is a long-shot however, I am stuck with the following problem: I have the output from ls -la, and I want to sort some of that data out by using AWK to filter it. ls -la | awk -f scriptname.awk Input: For example: drwxr-xr-x 3 user users 4096 2010-03-14 20:15 bin/... (5 Replies)
Discussion started by: abciscool
5 Replies

6. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

7. Shell Programming and Scripting

History for custom BASH function

Hello all, I have a bash function that opens Safari (I'm on OS X) with a specified argument. Here it is : function safari { #Safari bash function TLDS=( "http://www." ".com" ".org" ".net" ".gov" ".edu" ) if ; then open -a Safari ${TLDS}$2${TLDS} elif ; then open -a Safari... (0 Replies)
Discussion started by: inquen
0 Replies

8. UNIX for Dummies Questions & Answers

How to print largest and smallest number.

Hey. This is pretty easy stuff but I'm learning the basics of Unix at the moment so keep that in mind. I have to: 1) Write a C-shell script to monitor user activity on the server for 13 minutes. 2) Then print the smallest and largest number of users during these 13 minutes. I have this: 1)... (2 Replies)
Discussion started by: amp10388
2 Replies

9. Shell Programming and Scripting

How to print a % within a printf() function using awk

Here is the code I'm using { printf("%11d %4.2f\% %4.2f\%\n", $1,$2,$3); } I want the output to look something like 1235415234 12.24% 52.46% Instead it looks something like 319203842 42.27\%4.2f\% How do I just print a "%" without awk or printf thinking I'm trying to do... (1 Reply)
Discussion started by: Awanka
1 Replies

10. Programming

C function to test string or integer

Hi everyone , Is there any predefined C function that tests whether an input is string or an integer? Thank's in advance :) (3 Replies)
Discussion started by: qqq
3 Replies
Login or Register to Ask a Question