Reputation: 1551
I want to sort a file which has a particular delimiter in every line. I want to sort the lines such that sorting starts from that delimeter and sorts only according to numbers.
file is like this:
adf234sdf:nzzs13245ekeke
zdkfjs:ndsd34352jejs
mkd45fei:znnd11122iens
output should be:
mkd45fei:znnd11122iens
adf234sdf:nzzs13245ekeke
zdkfjs:ndsd34352jejs
Upvotes: 0
Views: 98
Reputation: 40748
In gawk
there is an asort
function, and you could use:
gawk -f sort.awk data.txt
where data.txt
is your input file, and sort.awk
is
{
line[NR]=$0;
match($0,/:[^0-9]*([0-9]*)/,a)
nn[NR]=a[1]" "NR
}
END {
N=asort (nn);
for (i=1; i<=N; i++) {
split(nn[i],c," ")
ind=c[2]
print line[ind];
}
}
Upvotes: 1
Reputation: 289725
This can be an approach, based on this idea:
$ sed -r 's/([^:]*):([a-z]*)([0-9]*)(.*)/\1:\2-\3\4/g' a | sort -t- -k2,2 | tr -d '-'
mkdfei:aa11122iens
adf234sdf:tt13245ekeke
zdkfjs:aa34352jejs
By pieces:
$ sed -r 's/([^:]*):([a-z]*)([0-9]*)(.*)/\1:\2-\3\4/g' a
adf234sdf:tt-13245ekeke
zdkfjs:aa-34352jejs
mkdfei:aa-11122iens
$ sed -r 's/([^:]*):([a-z]*)([0-9]*)(.*)/\1:\2-\3\4/g' a | sort -t- -k2,2
mkdfei:aa-11122iens
adf234sdf:tt-13245ekeke
zdkfjs:aa-34352jejs
$ sed -r 's/([^:]*):([a-z]*)([0-9]*)(.*)/\1:\2-\3\4/g' a | sort -t- -k2,2 | tr -d '-'
mkdfei:aa11122iens
adf234sdf:tt13245ekeke
zdkfjs:aa34352jejs
So what we do is to add a -
character before the first number. Then we sort based on that character and finally delete -
back (tr -d '-'
).
Upvotes: 2
Reputation: 33327
Use the -t
option to set the delimiter:
$ sort -t: -nk2,2 file
mkdfei:11122iens
adf234sdf:13245ekeke
zdkfjs:34352jejs
Upvotes: 2