krazyito65
krazyito65

Reputation: 95

How do I sort by date in a filename when files have different prefixes in bash?

I have an array of files (with path, path was removed and file name slightly modified for sensitive material), where I want to sort by the date inside the file name (but it sorts together all the prefixes first)

A_20160406_161734_083.txt
A_20160406_162033_756.txt
A_20160406_182413_069.txt
A_20160426_093015_846.txt
S_20160406_161741_568.txt
S_20160406_185235_774.txt
S_20160426_093019_852.txt
D__20160407_125904_986.txt
D__20160422_163704_889.txt
D__20160422_173021_513.txt
D__20160422_185627_116.txt
D__20160425_175231_627.txt
D__20160425_210242_615.txt
D__20160426_092631_837.txt
D__20160503_161921_802.txt
D__20160512_122039_138.txt
C_20160406_185205_258.txt
C_20160426_092920_788.txt

Here they are sorted by the prefix, how can I instead sort by the date?

Ways I have thought of is saving the names without the prefix, but the problem is getting the prefix back on correctly. (I need to see the prefix as well)

Bonus but not as needed: Separate different dates (not hours) by a '-----'

Upvotes: 2

Views: 652

Answers (1)

karakfa
karakfa

Reputation: 67507

another sed solution

sed -r 's/(_+)/\1 /' file | sort -k2 | sed 's/_ /_/'

separate the first possibly repeated _ chars, sort, join back.

sed -r 's/(_+)/\1 /' pieces: (_+)takes the first (notice no g suffix) longest _ (since greedy) string assigns to group \1 replaces with the first matched group (namely the _ string) and space.

sed 's/_ /_/' replaces the first _ (underscore space) match with _ (underscore only) meaning remove the space.

pipe the sed output to

... |  awk -F'_+' 'p!=$2{if(NR>1)print "------";p=$2}1'

A_20160406_161734_083.txt
S_20160406_161741_568.txt
A_20160406_162033_756.txt
A_20160406_182413_069.txt
C_20160406_185205_258.txt
S_20160406_185235_774.txt
------
D__20160407_125904_986.txt
------
D__20160422_163704_889.txt
D__20160422_173021_513.txt
D__20160422_185627_116.txt
------
D__20160425_175231_627.txt
D__20160425_210242_615.txt
------
D__20160426_092631_837.txt
C_20160426_092920_788.txt
A_20160426_093015_846.txt
S_20160426_093019_852.txt
------
D__20160503_161921_802.txt
------
D__20160512_122039_138.txt

the whole thing can be converted to gawk as well.

Upvotes: 3

Related Questions