Friday, 24 August 2018

How to sort content of a file based on multiple columns in UNIX


For e.g.

We have below data in a file and we want to sort based on the date.
We can sort this by :
 1st sort based on YYYY   ( -k 4n ) Sort 4th column based on number
 2nd sort based on MON    (-k 3M)  Sort 3rd column based on Month
 3rd sort based on DD
 4th sort based on TIME

test.txt
--------
Sun,  20  Aug 2018 23:11:10 GMT   1.0.0-MASTER-20180801-171
Wed, 22  Mar 2019 22:11:10 GMT  1.0.0-20180823-263
Mon, 22  Apr 2018 21:11:10 GMT  1.0.0-20180823-265
Wed, 28  Feb 2018 21:11:09 GMT  1.0.0-20180823-263
Tue,  28  Feb 2018 21:11:10 GMT   1.0.0-20180823-263
Wed, 21  Jan 2020 23:11:10 GMT  1.0.0-20180823-243


sort -k 4n -k 3M -k 2n -k 5n test.txt > sortedTest.txt


No comments:

Post a Comment

Thank You for your valuable comment

Difference between class level and object locking and static object lock

1) Class level locking will lock entire class, so no other thread can access any of other synchronized blocks. 2) Object locking will lo...