I want to sort a list on the datetime portion of its name.
Is this possible using sort? I cannot specify the sort column as the column may vary as shown in sample input below.
swid_ds_install_user_20171227172654_20425.log
package_user_20171227172949_5627.log
swid_state_definition_user_20171227162839_6515.log
swid_ds_install_user_20171227172732_23839.log
swid_appsrv_stop_user_20171227172258_27116.log
package_user_20171227172610_16198.log
swid_state_definition_user_20171227172344_322.log
package_user_20171227233634_23845.log
package_user_20171227162858_7082.log
I can reverse the order of fields through e.g.
awk -F_ '{for (i=NF;i>0;i--){printf $i"_"};printf "\n"}'
then sort with -d_ -k2,2 then reverse order of fields back to retain the original file name - purging residual delimiters with e.g. sed - but this gets awkward.
awk -F_ '{for (i=NF;i>0;i--){printf $i"_"};printf "\n"}' | sort -t'_' -k2,2 \
| awk -F_ '{for (i=NF;i>0;i--){printf $i"_"};printf "\n"}' | sed 's/^_//' \
| sed 's/_$//'
How would you approach this?
I was thinking along the lines of using sed to break out the datetime portion via regex and pipe that into sort and then use some built-in to recover the full filename and not only the matched regex, when printing the output.
Hoping I didn't produce another duplicate, I cannot really summarise the problem statement