If you have GNU sed
then you can pipe the output of grep
to it.
grep -E '(First|Last) Name' file | sed 'N;s/\n/ /' | sort -k8 | sed 's/Last/\nLast/'
First Name : cc
Last Name : ss
First Name : aa
Last Name : tt
grep -E '(First|Last) Name' file
will grep the lines with First and Last Namessed 'N;s/\n/ /'
will add the append the Last Name to First Namesort -k8
will sort the 8th field which is Last Name valuesed 's/Last/\nLast/'
will place them on separate lines
If you don't have GNU sed
then your other option would be to use xargs
:
grep -E '(First|Last) Name' file | xargs -n8 | sort -k8 | xargs -n4
First Name : cc
Last Name : ss
First Name : aa
Last Name : tt
Note: This assumes that your data has First Name and Last Names for every entries. xargs
solution assumes that your First Name has no Middle Name