AppleScript Terminal App to sort IP Addresses - terminal

Basically I want to be able to have an AppleScript terminal application that prompts for a text file that contains IP addresses, then sort the IP addresses, and output the result.
This is the code that I have so far:
do shell script "sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4" & (choose file)
No idea where to go from here really.
Getting the following error, as well:
sort: stray character in field spec: invalid field specification `4,4Macintosh'
EDIT: I figured it out, decided to just save the sorted output back into the original file.
Here is the final code:
do shell script "sort -n -t . -k 1,1 -k 2,2 -k 3,3 -k 4,4 /Users/<USERNAME>/Dev/IP-Blacklist/blacklist.txt -o /Users/<USERNAME>/Dev/IP-Blacklist/blacklist.txt"
display dialog "Sorting Complete!"

Related

Fill a argument with tab file values in bash

I need to creat a bash file in order to run a certain command on a server.
Here is one of the lines
Programm/programm.pl -k 1 -q --acc_number
where --acc_number needs a Comma-separated list of accession numbers, e.g. --acc_number Number13JJ2,Number0090D93,Number088DF.
but I actually have a file calle file_acc_number where I have each of the accession number in line such as :
Number13JJ2
Number0090D93
Number088DF
does someone have an idea how to parse this tab file and to directly put the accessio number in a comma-separated way and get :
Programm/programm.pl -k 1 -q --acc_number Number13JJ2,Number0090D93,Number088DF
Thank you for your help
Try using paste:
Programm/programm.pl -k 1 -q --acc_number `paste -s -d, file_acc_number`
Try running paste -s -d, file_acc_number first to understand whether you get what you require.
with an inline expansion maybe? Like this
Programm/programm.pl -k 1 -q --acc_number $(sed -z 's/\n/,/g' file_acc_number)
Make sure your file "file_acc_number" has no "new line" at the end of it.
With this, you will replace the "new line" character with a comma on the fly without affecting the original file.

BASH: sort email address list by domain

I would like to sort an email address list in a file by domain in bash.
$ cat file.txt
abc#abc.net
bbb#aaa.org
aba#aaa.com
aaa#aaa.com
ccc#abb.com
aba#abb.com
abc#abc.com
I tried with sort but it sorts only beginning with the username.
$ sort file.txt
aaa#aaa.com
aba#aaa.com
aba#abb.com
abc#abc.com
abc#abc.net
bbb#aaa.org
ccc#abb.com
I would like to sort first domain then username.
$ sort -t # -k2 file
aaa#aaa.com
aba#aaa.com
bbb#aaa.org
aba#abb.com
ccc#abb.com
abc#abc.com
abc#abc.net
man sort:
-t, --field-separator=SEP
use SEP instead of non-blank to blank transition
-k, --key=KEYDEF
sort via a key; KEYDEF gives location and type

How does "sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n" work?

I've come across this command which sorts the hosts file by ip, but I just can't break down the syntax so it would make sense. All I understood was that the first part means that the dot is used as delimeter, and then I got lost in all the commas and the rest of the arguments:
$ sort -t . -k 1,1n -k 2,2n -k 3,3n -k 4,4n /etc/hosts
127.0.0.1 localhost.localdomain localhost
192.168.100.101 site1.com dev-db
192.168.100.102 site2.com prod-db
192.168.101.20 site3.com dev-web
192.168.101.21 site4.com prod-web
Thanks.
-k designates a field-range. 1,1 (and the others respectively) express just one field, assuring that the sorting happens with precedence from left to right, and then within the already sorted subset. the n tacked on means "sort the column numerically".

reverse unix sort on non-existent field

I have a largish file with lines like this: (^I represents a tab, $ end-of-line)
2^IElaeocarpus williamsianus^I48$
4^I$
6^I$
8^I$
10^I$
12^I$
14^IElaeocarpus hookerianus^I73$
16^IElaeocarpus kirtonii^I111$
20^I$
22^ITetratheca juncea^I66$
42^IMalagasy giant rat^I401$
and I want to sort the lines so that those with the highest number in the 3rd field (i.e. after the 2nd tab) come first, i.e.
42^IMalagasy giant rat^I401$
16^IElaeocarpus kirtonii^I111$
14^IElaeocarpus hookerianus^I73$
22^ITetratheca juncea^I66$
2^IElaeocarpus williamsianus^I48$
4^I$
6^I$
8^I$
10^I$
12^I$
20^I$
(I don't care about the order of the lines with no field 3). So I assumed something like the following would work
sort -r -t $'\t' -k 3,3n myfile
but it doesn't (GNU sort, OS X 10.9). I feel I'm being stupid. What's the correct incantation?
You need to add a modifier to your -k parameter, not the command line parameter.
So something in these lines should do the trick:
sort -t $'\t' -k 3,3nr myfile
It seems that it isn't n what you want, but g.
sort -t $'\t' test.txt -k 3.2gr
The dot specifies in the key at which character to start comparing.
As favoretti pointed out, what you want to reverse is by that column, so you apply the modifier there.

Stable sorting two files into one with the duplicates

I've been trying to sort two files and get the output.
say for file 1:
102310863||7097881||6845123||271640||06007709532577||||
102310875||7092992||6840818||023740||10034500635650||||
and file 2:
102310863||7097881||6845193||271640||06007709532577||||
102310875||7092992||6840808||023740||10034500635650||||
The desired output is:
102310863||7097881||6845123||271640||06007709532577||||
102310863||7097881||6845193||271640||06007709532577||||
102310875||7092992||6840818||023740||10034500635650||||
102310875||7092992||6840808||023740||10034500635650||||
I've been trying to use the sort command
sort -t \| -n -k1,1 t1.txt t2.txt
but it is giving me the output
102310863||7097881||6845123||271640||06007709532577||||
102310863||7097881||6845193||271640||06007709532577||||
102310875||7092992||6840808||023740||10034500635650||||
102310875||7092992||6840818||023740||10034500635650||||
which is not what I want because original file order is not preserved.
Is there any other way of doing it to get the desired output?
Using the -s flag performs a stable sort.
sort -s -t \| -k1,1 t1.txt t2.txt
From man sort:
-s, --stable
stabilize sort by disabling last-resort comparison

Resources