Get min from a column conditionally


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get min from a column conditionally
# 1  
Old 12-08-2010
Question Get min from a column conditionally

hi,

i have a file with folowing content:

Code:
STORAGE PERCENTAGE FLAG:
/storage_01 64% 0
/storage_02 17% 1
/storage_03 10% 0
/storage_04 50% 1

I need to get the value of STORAGE from those with FLAG=0 and which has the min PERCENTAGE

i am able to get the STORAGE corresponding to the min PERCENTAGE from this :

Code:
 
awk 'NR==2{min=$2}NR>2{if (min > $2) min=$2}END{print "STORAGE with min PERCENTAGE :", $1}' file.dat

But i need to select only those rows where FLAG has value 0 Smilie

Please let me know how to do that...

Thanks in advance
# 2  
Old 12-08-2010
Code:
awk '
NR==1{min=100;next}
$3==0 && int($2)<min{min=int($2); s=$1}
END{print "STORAGE with min PERCENTAGE : " s, min "%"}' file

# 3  
Old 12-08-2010
Hi, IMO you solution would need to be modified to something like this:
Code:
awk 'BEGIN{min=100}NR>1{if (min > $2+0) {min=$2+0;st=$1}}END{print "STORAGE with min PERCENTAGE :", st}' infile

The +0 's are necessary to force $2 into numerical context to get rid of the %-sign, otherwise the order would be alphabetical and minimum storage would be number storage_03
To include the flag value you can do this.
Code:
awk 'BEGIN{min=100}NR>1{if ($3==0) if (min > $2+0) {min=$2+0;st=$1}}END{print "STORAGE with min PERCENTAGE :", st}' infile

It could then be written differently as:
Code:
awk 'BEGIN{min=100}NR>1 && !$3 && min>$2+0{min=$2+0;st=$1}END{print "STORAGE with min PERCENTAGE :", st}' infile

But that is just a matter of taste..

Last edited by Scrutinizer; 12-08-2010 at 04:48 AM.. Reason: $3 should be 0 not 1. Thanks Franklin!
# 4  
Old 12-08-2010
Question

Thank you all..

Can you let me know how can i get the Storage name in variable for further processing??
# 5  
Old 12-08-2010
@Scrutinizer, the condition is:

Quote:
But i need to select only those rows where FLAG has value 0
# 6  
Old 12-08-2010
Oops, I corrected it in my post Smilie
# 7  
Old 12-08-2010
Quote:
Originally Posted by kichu
Thank you all..

Can you let me know how can i get the Storage name in variable for further processing??
Something like:
Code:
storagename=$(awk '
NR==1{min=100;next}
$3==0 && int($2)<min{min=int($2); s=$1}
END{print s}' file)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Picking up files conditionally

Hi I have a scenario: I have a directory say DIR1 (no sub directories) and have few files in that directory as given below: app-cnd-imp-20150820.txt app-cxyzm-imp-20150820.txt app-petco-imp-20150820.txt app-mobility-imp-20150820.txt app-mobility-imp-20150821.txt... (7 Replies)
Discussion started by: Saanvi1
7 Replies

2. Shell Programming and Scripting

Get min and max value in column

Gents, I have a big file file like this. 5100010002 5100010004 5100010006 5100010008 5100010010 5100010012 5102010002 5102010004 5102010006 5102010008 5102010010 5102010012 The file is sorted and I would like to find the min and max value, taking in the consideration key1... (3 Replies)
Discussion started by: jiam912
3 Replies

3. Shell Programming and Scripting

Print min and max value from two column

Dear All, I have data like this, input: 1254 10125 1254 10126 1254 10127 1254 10128 1254 10129 1255 10130 1255 10131 1255 10132 1255 10133 1256 10134 1256 10135 1256 10137... (3 Replies)
Discussion started by: aksin
3 Replies

4. Shell Programming and Scripting

How to conditionally replace a pattern?

Hi, How to replace only the function calls with a new name and skip the function definition and declarations. consider the following code. There are 2 functions defined here returnint and returnvoid. I need to replace returnint with giveint and returnvoid with givevoid only in the function... (2 Replies)
Discussion started by: i.srini89
2 Replies

5. UNIX for Dummies Questions & Answers

How to conditionally replace a pattern?

Hi, How to replace only the function calls with a new name and skip the function definition and declarations. consider the following code. There are 2 functions defined here returnint and returnvoid. I need to replace returnint with giveint and returnvoid with givevoid only in the function... (1 Reply)
Discussion started by: i.srini89
1 Replies

6. Shell Programming and Scripting

to find min and max value for each column!

Hello Experts, I have got a txt files which has multiple columns, I want to get the max, min and diff (max-min) for each column in the same txt file. Example: cat file.txt a 1 4 b 2 5 c 3 6 I want ouput like: cat file.txt a 1 4 b 2 5 c 3 6 Max 3 6 Min 1 4 Diff 2 2 awk 'min=="" ||... (4 Replies)
Discussion started by: dixits
4 Replies

7. Shell Programming and Scripting

Conditionally delete last X lines

delete last X lines,which start with + example file: test1 test2 remove1 remove2 one liner shell is preferred. (8 Replies)
Discussion started by: honglus
8 Replies

8. Shell Programming and Scripting

Conditionally prepending text

I am currently writing a script to compare a file list created over an FTP connection to a local directory. I have cleaned the FTP file list up so that I just have a raw list of filenames however due to the directory structure employed (both locally and on the ftp site) I need to prepend each line... (6 Replies)
Discussion started by: Dal
6 Replies

9. UNIX for Dummies Questions & Answers

Conditionally joining lines in vi

I've done this before but I can't remember how. Too long away from vi. I want to do a search are replace, but I want the replace to be a join. Example see spot run see spot walk see spot run fast see spot hop %s/run$/<somehow perform a join with the next line>/g so the results... (0 Replies)
Discussion started by: ifermon
0 Replies

10. Shell Programming and Scripting

Email from script conditionally

I have a script that is run from the Cron 3 times an hour, here is the cron line: 02,22,42 7-18 * * 1-5 /hci/TEST/bin/myscript.ksh TEST 1>/hci/TEST/logs/myscript.info 2>/hci/TEST/logs/myscript I am curious as to whether the time parameters from cron, ( 02, 22, 42 etc) can be accessed from the... (2 Replies)
Discussion started by: dfb500
2 Replies
Login or Register to Ask a Question