Searching max


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching max
# 8  
Old 10-05-2013
Quote:
Originally Posted by Manu1234567
Basically I have a file num1 which contains IP addresses
Code:
134.123.3.236
134.123.25.231
134.123.3.235
134.123.2.236
134.123.5.34
134.123.25.237
134.123.21.236
134.123.23.236


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).

I tried to split up IP address in 4 parts such as A B C D... And then I work only with C and D but my code gives me C =2 which is not max ;( D does now work at all ;( Help me please to fix my little piece of code ;( thanks

Code:
max=0
sort num1 | { IFS="." read A B C D;
for i in ` wc -l `
 do
if [ "${C}" > "${max}" ]
then
max="${C}"
fi

echo $max; done 
for i in ` wc -l `
do 
if [ $max "$D" > "$lastmax"]
then 
lastmax="$D"
fi done

echo $lastmax
 }

}~
There are several problems here, including, but not necessarily limited to:
  1. Sorting a file alphanumerically using the entire input line as the only sort key doesn't serve any useful purpose when you're trying to compare numeric values in the 3rd and 4th period separated fields.
  2. Your 1st for loop's do doesn't have a matching done.
  3. The wc -l command in both of your for loops will hang waiting for the user to type in input (since no file operands are given on the command line).
  4. I don't see how counting the number of lines in a file (which is what wc -l does) will be of any use in solving this problem.
  5. In the test command [ $max "$D" > "$lastmax"] you need a space before the closing square bracket.
  6. In both of your test commands, the > operator performs an alphanumeric comparison. You need to use the -gt operator to compare numeric comparisons. (For example the command:
    Code:
    if [ "7" > "100 ]
    then    echo "7 is greater than 100"
    fi

    prints:
    Code:
    7 is greater than 100

    but the command:
    Code:
    if [ "7" -gt "100 ]
    then    echo "7 is greater than 100"
    fi

    produces no output.)
The scripts that RudiC and I provided before should give you alternative solutions to this problem.
# 9  
Old 10-05-2013
Quote:
Originally Posted by CarloM
Oops. That was my intention, but I neglected to reset maxoct4, as in Don's corrected version.


Do you want to maximum over all files? Just pass all the filenames to the awk (or sort) command.
I guess i need add smth else .....

Code:
sort "/Users/Manu/files/*" | 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

Moderator's Comments:
Mod Comment We are serious about using CODE tags. Leaving out the code tags makes the above (poorly formatted single-line) command appear to be a multi-line command with syntax errors.

Last edited by Don Cragun; 10-05-2013 at 05:21 PM..
# 10  
Old 10-05-2013
You don't need the sort with the awk solution - just change ips.txt to /Users/Manu/files/*.

Also, Don's version is more efficient.

Last edited by CarloM; 10-05-2013 at 06:16 PM.. Reason: yet more errors...
# 11  
Old 10-05-2013
Quote:
Originally Posted by Manu1234567
I guess i need add smth else .....

Code:
sort "/Users/Manu/files/*" | 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

Moderator's Comments:
Mod Comment We are serious about using CODE tags. Leaving out the code tags makes the above (poorly formatted single-line) command appear to be a multi-line command with syntax errors.
No, combining bits and pieces of RudiC's suggested code, and CarloM's (or my corrected version of CarloM's) code gives you a hybrid solution that does runs slower and only considers the contents of one file.

I would still suggest my solution (slightly modified to use your new set of files):
Code:
awk -F '[.]' '
$3 > m3 {m3 = $3; m4 = $4; next}
$3 == m3 && $4 > m4 {m4 = $4}
END {print "xxx.xxx." m3 "." m4}
' /Users/Manu/files/*

but I may be biased.

Note that if you did want to sort a list of files in the directory /usrs/Manu/files, you would have to leave off the double quotes in the command:
Code:
sort /Users/Manu/files/*

With the command:
Code:
sort "/Users/Manu/files/*"

it will only sort the single file named * in that directory or produce a file not found diagnostic if that file doesn't exist.
# 12  
Old 10-05-2013
Thank you, now its clear. One more thing... I want to have a condition statement that if fourth oct of IP is 254 then just make it 0 and increment third oct. So xxx.xxx.3.254 => next xxx.xxx.4.0
I just added condition to awk but it doesnt seem to work...

Code:
awk -F '[.]' '
$3 > m3 {m3 = $3; m4 = $4; next}
$3 == m3 && $4 > m4 {m4 = $4}

$m4 ==254 { m4=0; m3 = $m3 +1}

END

 {print "134.123." m3 "." m4}
' /Users/Manu/files/hosts/*


Last edited by Manu1234567; 10-05-2013 at 07:25 PM..
# 13  
Old 10-05-2013
Try something like:
Code:
awk -F '[.]' '
$3 > m3 {m3 = $3; m4 = $4; next}
$3 == m3 && $4 > m4 {m4 = $4}
END {   if(m4 == 254) {m4 = 0; m3++}
        print "134.123." m3 "." m4
}' /Users/Manu/files/hosts/*

# 14  
Old 10-05-2013
Code:
$m4 ==254 { m4=0; m3 = $m3 +1}

In awk $ is used to access fields, not variables - if m4 is 254, $m4 is a reference to (empty) field 254.

Also, consider what you want to do in this case if octet 3 is already 255.
This User Gave Thanks to CarloM For This Post:
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