Need some assistance with LINUXUNIX file sorting Consider th
Need some assistance with LINUX/UNIX file sorting
Consider the file whodb created with this command: -bash-3.2$ who > whodb
Part 1: Please show the command to: sort it by date (primary key) and time (secondary key) logged in.
Part 2: Please show the command to: create a new file called useron containing username, date, and time logged in.
Solution
Taking sample data:
Code Date TIME
AHOS05 23/12/2016 10AM
AHOS06 15/12/2016 2PM
AHOS62 29/12/2016 10AM
AHOS64 23/12/2016 2PM
AHOS70 26/12/2016 10AM
AZFT01 06/12/2016 10AM
AHOS73 11/12/2016 2PM
AMHOS051 05/12/2016 10AM
AMHOS041 20/12/2016 2PM
AMHOS042 27/12/2016 2PM
AMHOS053 11/12/2016 10AM
AMHOS043 30/12/2016 2PM
A) Try using sort command to display lines of its input listed in sorted order.
Special option is there which is called -M to sort months. Total 3 fields are there in which First one is some sort of code, Second one is actual date in DD/MM/YYYY format and third one is time.
As question says, sorting needs to be done using date and time.
So,You need to sort 2nd and 3rd column using -k option of sort commands as follows:
$ sort -k 2,2n -k 3 whodb
Sample Output
AMHOS051 05/12/2016 10AM
AZFT01 06/12/2016 10AM
AMHOS053 11/12/2016 10AM
AHOS73 11/12/2016 2PM
AHOS06 15/12/2016 2PM
AMHOS041 20/12/2016 2PM
AHOS05 23/12/2016 10AM
AHOS64 23/12/2016 2PM
AHOS70 26/12/2016 10AM
AMHOS042 27/12/2016 2PM
AHOS62 29/12/2016 10AM
AMHOS043 30/12/2016 2PM
Where,
-> -k 2,2n -k 3 : Sort data using the given column number.
The option -k 2,2n -k 3 will sorts each column.
First, it will sort 2nd column (date dd field) and then 3rd column (time).
-> whodb : Input file
