Bambi Bunny
Bambi Bunny

Reputation: 1418

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?

Upvotes: 1

Views: 69

Answers (2)

David W.
David W.

Reputation: 107080

Why not simply just find all of the UMSATZ* files by time, and then list the AUSZUG* one for each of the UMSATZ* you find:

ls -tr UMSATZ* | while read file
do
    sans_prefix=${file#UMZATZ}
    ls -ld *$sans_prefix
done

Or something like this... The ${file#UMSATZ} removes the prefix UMSATZ from $file, so that if $file equals UMSATZ_1111111122.TXT.GPG, then $sans_prefix equals _1111111122.TXT.GPG. The *$sans_prefix should list both files with the same numeric suffix.

Upvotes: 0

user1978011
user1978011

Reputation: 3589

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.

Upvotes: 1

Related Questions