Issue
I have a file with the following data in it:
adam
humanities
castiel
sciences
antwon
sciences
dmitri
informatics
zoe
mathematics
bernard
economics
I want to be able to sort the file w.r.t the names of the people so that the output looks like so:
adam
humanities
antwon
sciences
bernard
economics
castiel
sciences
dmitri
informatics
zoe
mathematics
cat filename | sort
sorts all the data including the subjects. How do I sort it with the names of people?
Solution
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"}'
Answered By - Jotne Answer Checked By - Mildred Charles (WPSolving Admin)