Its Soni
Its Soni

Reputation: 41

Shell | Sort Date and Month in Ascending order

I wanted to display/sort the file records in Ascending order of Date and Month or if there are any equal data values they should list in the very next column in ascending order.

Date & Month to sort: (current scenario)

File content:

How would I can achieve below following results?

I tried using sort: (not working) sort -n sortfile.txt

Upvotes: 0

Views: 229

Answers (1)

David C. Rankin
David C. Rankin

Reputation: 84561

You can use sort, but you will need to specify the field-seperator -t '-' so that fields are separated by '-' and then specify the keydef to sort on the 5th field beginning with the 4th character and then again with the 1st character and finally a version sort on field 6 if all else is equal. That would be:

sort -t '-' -k5.4n -k5.1n -k6V  contents

Providing full start and stop characters within each keydef can be done as:

sort -t '-' -k5.4n,5.5 -k5.1n,5.2 -k6V  contents

(though for this data the output isn't changed)

Example Use/Output

$ sort -t '-' -k5.4n -k5.1n -k6V  contents
ver>0.1.1-XYZ-LOK-BR-19.01-v1.0-5-8a8d7dd/ver>
ver>0.1.1-DXD-UIJ-BR-02.02-v1.0-4-9o2k4wk/ver>
ver>0.1.1-ABC-XYA-BR-03.02-v1.0-1-4d4f3dd/ver>

Upvotes: 2

Related Questions