Monday, October 29, 2012

Bash one-liner: Sort files by scientific number in the filename

The following code takes a list of files and sorts them by a number in the filename. For example data_0.0e0, data_1.0e3, and data_5.0e1 will be sorted to data_0.0e0, data_5.0e-1, data_1.0e3. The output could be piped into another command that needs the ordered files for whatever reason. ls data_* | sed -e 's/data_//g' | sort -g | sed -e 's/^/data_/g'