Skip to content
Advertisement

list file key-pair sorted by time

I need to sort a filename pair sorted by time. Every file starts with AUSZUG or UMSATZ (and some numbers after). But the problem is that I cannot sort it by just time because some files can be uploaded (created) not as pair. For example, default sort:

ls -ltr --time-style "long-iso" {UMSATZ,AUSZUG}* | head -n 20
-rw-r--r-- 1 user group 0 2015-05-04 14:59 UMSATZ_2107381391.TXT.GPG
-rw-r--r-- 1 user group 0 2015-05-04 14:59 AUSZUG_2107381391.TXT.GPG
-rw-r--r-- 1 user group 0 2015-05-04 15:00 UMSATZ_1111111122.TXT.GPG
-rw-r--r-- 1 user group 0 2015-05-04 15:00 AUSZUG_1111111122.TXT.GPG
-rw-r--r-- 1 user group 0 2015-05-04 15:00 UMSATZ_9785745789.TXT.GPG
-rw-r--r-- 1 user group 0 2015-05-04 15:00 AUSZUG_9785745789.TXT.GPG
-rw-r--r-- 1 user group 0 2015-05-04 15:17 UMSATZ_6785745789.TXT.GPG
-rw-r--r-- 1 user group 0 2015-05-04 15:17 AUSZUG_6785745789.TXT.GPG
-rw-r--r-- 1 user group 0 2015-05-04 15:45 AUSZUG_8785745789.TXT.GPG
-rw-r--r-- 1 user group 0 2015-05-04 15:45 AUSZUG_5785745789.TXT.GPG
-rw-r--r-- 1 user group 0 2015-05-04 15:45 UMSATZ_8785745789.TXT.GPG
-rw-r--r-- 1 user group 0 2015-05-04 15:45 UMSATZ_5785745789.TXT.GPG

You can see, UMSATZ pair for AUSZUG_5785745789.TXT.GPG are in last line

I can solve this by sort

ls -ltr --time-style "long-iso" {UMSATZ,AUSZUG}* | sort -t "_" -k 2 | head -n 20
-rw-r--r-- 1 user group 0 2015-05-04 15:00 AUSZUG_1111111122.TXT.GPG
-rw-r--r-- 1 user group 0 2015-05-04 15:00 UMSATZ_1111111122.TXT.GPG
-rw-r--r-- 1 user group 0 2015-05-04 14:59 AUSZUG_2107381391.TXT.GPG
-rw-r--r-- 1 user group 0 2015-05-04 14:59 UMSATZ_2107381391.TXT.GPG
-rw-r--r-- 1 user group 0 2015-05-04 15:45 AUSZUG_5785745789.TXT.GPG
-rw-r--r-- 1 user group 0 2015-05-04 15:45 UMSATZ_5785745789.TXT.GPG
-rw-r--r-- 1 user group 0 2015-05-04 15:17 AUSZUG_6785745789.TXT.GPG
-rw-r--r-- 1 user group 0 2015-05-04 15:17 UMSATZ_6785745789.TXT.GPG
-rw-r--r-- 1 user group 0 2015-05-04 15:45 AUSZUG_8785745789.TXT.GPG
-rw-r--r-- 1 user group 0 2015-05-04 15:45 UMSATZ_8785745789.TXT.GPG
-rw-r--r-- 1 user group 0 2015-05-04 15:00 AUSZUG_9785745789.TXT.GPG
-rw-r--r-- 1 user group 0 2015-05-04 15:00 UMSATZ_9785745789.TXT.GPG

But now it’s not by the time 🙁 Anyone idea to sort it by AUSZUG-UMSATZ pair sorted by time?

Advertisement

Answer

I’m not sure if I got you right, I understand that you want to have a list of UMSATZ_* files sorted by time interlaced with the corresponding AUSZUG_* files. You can get this in bash with this one-liner:

ls -t UMSATZ_* | while read x; do echo "$x"; echo "AUSZUG${x#UMSATZ}"; done

The unusually looking ${x#UMSATZ} strips the string UMSATZ from the front of the x variable.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement