Linux shell 實現文本按 xxxx-xx-xx 日期排序展示

需要將以下內容,按照第一列的時間排序展示:

//test.txt

2020-01-14 test1
2019-12-23 test2
2019-07-11 test3
2019-07-16 test4
2019-09-01 test5
2019-09-10 test6
2019-12-23 test7
2019-11-16 test8
2019-10-11 test9
2019-11-22 test10

通過 awk 和 sort 配合實現:

cat test.txt | awk '{print $1 "-" $2}' | sort -n -k 1 -k 2 -k 3  -t '-'  | awk -F'-' '{print $1"-"$2"-"$3" "$4}'

輸出結果:

//test.txt 處理後結果

2019-07-11 test3
2019-07-16 test4
2019-09-01 test5
2019-09-10 test6
2019-10-11 test9
2019-11-16 test8
2019-11-22 test10
2019-12-23 test2
2019-12-23 test7
2020-01-14 test1
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章