Merging and sorting multiple files with "sort"
Solution 1:
Your key should be -k1.17n
and omit the -t
and the +17
.
Is there a space between the ID and the timestamp? Then the timestamp is field 2 and the key should be -k2
.
Solution 2:
man sort
reads:
-m, --merge merge already sorted files; do not sort
The '+' symbol doesn't show up in my man page for sort. So I don't know how you get +17. If you want to use the whole line, you do not need -t
or -k
, since the default is to start sorting from beginning of line, to end of line.
Solution 3:
I like these hard ones...this one got me thinking:
Essentially, it concatenates all the .txt files, separates them with colons (for sorting), sorts the second field (the r sorts newest first, take it out if you want newest last), and then removes the colons, showing the original line.
cat *.txt
| awk '{print substr($0,1,17)":"substr($0,18,14)":"substr($0,32)}'
| sort -t: -k2,2 -nr -s
| tr -d ':'
I've tested it with three 4-line .txt files.
First File
1234567890123456720100603104500Random text or data
2345678901234567820100602104500New Random Text
3456789012345678920100509213849Earlier Date
4567890123456789020100521195058InBetween Date
Second File
1234567890123456720100603124500File2 Random text or data
2345678901234567820100602124500File2 New Random Text
3456789012345678920100519213849File2 Earlier Date
4567890123456789020100523195058File2 InBetween Date
Third File
12345678901234567201106031045003Random text or data
23456789012345678201004021045003New Random Text
34567890123456789201007092138493Earlier Date
45678901234567890201005231950583InBetween Date
Results
12345678901234567201106031045003Random text or data
34567890123456789201007092138493Earlier Date
1234567890123456720100603124500File2 Random text or data
1234567890123456720100603104500Random text or data
2345678901234567820100602124500File2 New Random Text
2345678901234567820100602104500New Random Text
4567890123456789020100523195058File2 InBetween Date
45678901234567890201005231950583InBetween Date
4567890123456789020100521195058InBetween Date
3456789012345678920100519213849File2 Earlier Date
3456789012345678920100509213849Earlier Date
23456789012345678201004021045003New Random Text