Could you please try following and let me know if this helps you. Let's say we have following Input_file.
Then following code may help us to achieve as your request.
Output will be as follows.
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.
Could you please try following and let me know if this helps you.
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.
Then following code may help us to achieve as your request.
Output will be as follows.
Thanks,
R. Singh
`awk` function looks like this in a file name `fun.awk`:
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):
makes the command line:
produce the output:
And if the data sample suggested in post #2 is stored in a file named file, the command:
produces the output:
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:
or, MUCH more efficiently, as:
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:
This User Gave Thanks to Don Cragun For This Post:
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)
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)
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)
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)
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)
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)
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)