Searching max


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching max
# 1  
Old 10-05-2013
Searching max

Basically I have a file num1 which contains IP addresses
[/CODE]
I need to find max element of third number in IP address which is 25! And after that relating to max third number I need to find max fourth number which is 237 (6th line)

Last edited by Manu1234567; 10-10-2013 at 10:45 PM..
# 2  
Old 10-05-2013
Please use code tags as required by forum rules!

Try
Code:
sort -nrt"." -k3,3 -k4,4 file | { IFS="." read A B C D; printf "%s %s\n" $C, $D; }
25, 237

# 3  
Old 10-05-2013
An awk solution:
Code:
awk '($3 > maxoct3) { maxoct3 = $3; maxoct4 = $4 }
$3 ~ maxoct3 && ($4 > maxoct4) { maxoct4 = $4 }
END {printf "xxx.xxx.%s.%s\n", maxoct3, maxoct4}' FS=. ips.txt


Last edited by CarloM; 10-05-2013 at 05:09 PM.. Reason: Added missing reset of maxoct4
# 4  
Old 10-05-2013
@CarloM: I think he/she wanted the maxoct4 of all lines with maxoct3. If there were a line e.g. x.x.21.238 BEFORE maxoct3, that awk would print xxx.xxx.25.238, which is not contained in the input file.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 10-05-2013
Quote:
Originally Posted by RudiC
@CarloM: I think he/she wanted the maxoct4 of all lines with maxoct3. If there were a line e.g. x.x.21.238 BEFORE maxoct3, that awk would print xxx.xxx.25.238, which is not contained in the input file.
RudiC,
You're obviously correct. But, we could use CarloM's approach to the problem to avoid the need to sort the input:
Code:
awk -F '[.]' '
$3 > m3 {m3 = $3; m4 = $4; next}
$3 == m3 && $4 > m4 {m4 = $4}
END {print "xxx.xxx." m3 "." m4}
' num1

As always, if someone wants to try this on a Solaris/SunOS system, they need to use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 10-05-2013
Quote:
Originally Posted by Don Cragun
RudiC,
You're obviously correct. But, we could use CarloM's approach to the problem to avoid the need to sort the input:
Code:
awk -F '[.]' '
$3 > m3 {m3 = $3; m4 = $4; next}
$3 == m3 && $4 > m4 {m4 = $4}
END {print "xxx.xxx." m3 "." m4}
' num1

As always, if someone wants to try this on a Solaris/SunOS system, they need to use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.
What if each of these IPs contains in each file?

Last edited by Manu1234567; 10-10-2013 at 10:45 PM..
# 7  
Old 10-05-2013
Quote:
Originally Posted by RudiC
@CarloM: I think he/she wanted the maxoct4 of all lines with maxoct3.
Oops. That was my intention, but I neglected to reset maxoct4, as in Don's corrected version.

Quote:
Originally Posted by Manu1234567
What if each of these IPs contains in each file? I think I need to add only for loop right? I mean for example
134.123.3.236 in file1 134.123.25.231 in file2
134.123.3.235 in file 3 etc
Do you want to maximum over all files? Just pass all the filenames to the awk (or sort) command.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get the MAX value out of a column

I've the following data set. I would like to look at the column 3 and only use the rows which has the max value for column 3 Can we use the awk or sed to achieve it. 10 2 10 100 11 2 20 100 12 2 30 100 13 2 30 100 14 ... (7 Replies)
Discussion started by: rudoraj
7 Replies

2. Shell Programming and Scripting

searching a file with a specified text without using conventional file searching commands

without using conventional file searching commands like find etc, is it possible to locate a file if i just know that the file that i'm searching for contains a particular text like "Hello world" or something? (5 Replies)
Discussion started by: arindamlive
5 Replies

3. Linux

max password is blank

Hi All, I have this userid: # cat /etc/shadow | grep mysql mysql:$1$O12345678901234/.:123456:0::14::: Max password expiration is blank. What does it mean? If it's like this below mysql:$1$O12345678901234/.:123456:0:99999:14::: then there's no expiration. Thanks in advance for... (1 Reply)
Discussion started by: itik
1 Replies

4. Programming

int *ptr + max possible value

From reading my C book, Im aware that the integers have a maximum value which depends on what type of processor you are using (since they use 16-bit or 32-bit instructions). Now I know pointers are very flexible, since they can reference anything, but in the case of integer pointers, can they... (4 Replies)
Discussion started by: JamesGoh
4 Replies

5. Shell Programming and Scripting

Max NO of parameters

1> Can anyone tell me what is the maximum no of parameters that can be passed to a file 2> i have 100 files amongst which in 500 files i have a string which needs to be searched how would get the names of those files. (1 Reply)
Discussion started by: Shivdatta
1 Replies

6. Shell Programming and Scripting

max value

Hi All, I want to choose the maximum value from a column values. eg - 234 23 567 43 2456 5 678 8978 from thses values how to choose the max value ? thanks. roger. (3 Replies)
Discussion started by: Nayanajith
3 Replies

7. Shell Programming and Scripting

How i get the max value of a row?

I have a file like: <word> 5 <word> 3 <word> 5 <word> 2 <word> 6 <word> 8 <word> 12 and i need to know the max value of the second column, in this case 12. Plz help me! Actually i need the TOTAL, AVERANGE and MAX VALUE and i'm using this in... (10 Replies)
Discussion started by: Lestat
10 Replies

8. Shell Programming and Scripting

Finding max value

My code below is supposed to find which company had the most business and then print the appropriate fields from another file which are the companies ID number and name. I can loop through awk and display all the total amount of business for each company but I need help in only printing out the... (1 Reply)
Discussion started by: Enigma23
1 Replies

9. UNIX for Dummies Questions & Answers

Max I/O Size

My HP-UX 11.0 system is supporting an Oracle database. I have found a number of references on the Net to the "Max I/O size" in relation to setting Oracle parameters. How can I tell what my max i/o size is? I originally made the assumption that it was referring to my stripe size but now I think... (1 Reply)
Discussion started by: keelba
1 Replies
Login or Register to Ask a Question