Loading...

Sorting alphabetically in Bash

Answer #1 100 %

Using asorti in awk to sort the array of data

awk '{a[$1]=$2} END {n=asorti(a,c);for (i=1;i<=n;i++) print c[i] "\n" a[c[i]] "\n"}' RS= file
adam
humanities

antwon
sciences

bernard
economics

castiel
sciences

dmitri
informatics

zoe
mathematics

If your awk does not have asorti, try this:

awk '{print $1,$2}' RS="" file | sort | awk '{print $1 "\n" $2 "\n"}'

You’ll also like:


© 2022 CodeForDev.com -