Re: Sorting IP addresses
Re: Sorting IP addresses
- Subject: Re: Sorting IP addresses
- From: Graff <email@hidden>
- Date: Sun, 21 Dec 2003 14:33:36 -0500
Here is a breakdown:
sort
The name of the command-line application, of course
-n
Treat each field as a number rather than sorting on the first
character. Without this 123 would come before 52 since 1 comes before
5.
-t '.'
Defines the separation character used between fields, quoted just to be
sure it works, used single quotes because double quotes are used in
AppleScript to start and end text strings.
-k 1,1
Defines each sort field. The format is X.M,Y.N. X is the start field
(numbering starts at 1 for the first field), M is the character to use
from that field (left out because we are using -n and we want to use
the entire field). Y is the end field, N is the character from the end
field, if Y is the same as X then it will just use that field.
Supposedly the Y.N is not needed but I put it in for completeness. The
reason I used -k 1,1 -k 2,2 etc. is that if there is a tie on the first
-k definition it will use the second as a tie breaker, then the third,
then the forth, and so on. Since we have 4 quads in an IP number and
they go from most significant to least I used 4 fields.
unsortedfile
The file to read from, it must be in POSIX format. If you have a file
with a Finder path of "Macintosh HD:Users:Desktop:The File.txt" then
you need to format it as '/Macintosh HD/Users/Desktop/The File.txt' or
as /Macintosh\ HD/Users/Desktop/The\ File.txt
-o
This sets the file to write the sorted data to. The file path of the
write file should follow this command. If the file already exists it
will be overwritten, if it doesn't then it will be created.
sortedfile
Since this file follows the -o option it will be the write file. The
file path should be formatted the same as unsortedfile above.
- Ken
On Dec 21, 2003, at 9:22 AM, Bill wrote:
I changed it around a bit and this seems to work fine also:
sort -n -t '.' -k 1,1 -k 2,2 -k 3,3 -k 4,4 unsortedfile -o
sortedfile
Bravo :)
Any brief explanation now? I can't get the meaning of -k from the man
page & your example.
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.