I would like to extract the numeric part of the file names that begin with "hsli" and end with ".h5" in Bash on Ubuntu 14.04.1 64-bit LTS. My ls -l hsli*
output is as follows:
-rwxrwxrwx 1 ongun ongun 31392 Feb 26 13:04 hsli0.03.h5
-rwxrwxrwx 1 ongun ongun 31392 Feb 26 13:44 hsli0.042.h5
-rwxrwxrwx 1 ongun ongun 31392 Feb 26 14:24 hsli0.054.h5
-rwxrwxrwx 1 ongun ongun 31392 Feb 26 15:03 hsli0.066.h5
-rwxrwxrwx 1 ongun ongun 31392 Feb 26 15:42 hsli0.078.h5
-rwxrwxrwx 1 ongun ongun 31392 Feb 26 16:22 hsli0.09.h5
-rwxrwxrwx 1 ongun ongun 31392 Feb 26 17:02 hsli0.102.h5
-rwxrwxrwx 1 ongun ongun 31392 Feb 26 17:36 hsli0.114.h5
-rwxrwxrwx 1 ongun ongun 31392 Feb 26 17:58 hsli0.126.h5
-rwxrwxrwx 1 ongun ongun 31392 Feb 26 18:20 hsli0.138.h5
-rwxrwxrwx 1 ongun ongun 31392 Feb 26 18:42 hsli0.15.h5
They are already in ascending order and after a bit of manipulation I am able to get the file name for the first file with the following command. The command and the output follow below:
$ ls -l hsli* | head -1 | rev | cut -f 1 -d " " | rev
hsli0.03.h5
Now my aim is to extract 0.03
from here, how can I do so? I am not familiar with regular expressions and this seems like a hard case since there are 2 dots in the file name.
ls hsli* | head -1 | sed 's/[0-9]*\.[0-9]*//'
or evenls hsli* | head -1 | sed 's/[0-9.]\+/./'
– Costas Feb 26 '15 at 19:56hsli.h5
. – Vesnog Feb 26 '15 at 19:59\n
ewlines in the filenames, do:\ls -d ./hsli* | cut -d. -f3
for the whole list - addhead -n1
to the end. @Costas - you can drophead
if you just add a;q
to the tail of your command. – mikeserv Feb 26 '15 at 20:01ls
, you can do:set -- hsli*; set -- "${1#*.}"; echo "${1%.*}"
– mikeserv Feb 26 '15 at 20:0303
as the output not0.03
. Can I manually prepend a dot in the beginning, say withsed
? – Vesnog Feb 26 '15 at 20:05printf %.02f\\n ".$(earlier cmd)"
- it's probably better thanecho
anyway. Or for the second version just...;echo "0.${1%.*}"
. Oh, and maybe add a-s
switch tocut
so you only work with filenames that definitely contain the right amount of.
dots. – mikeserv Feb 26 '15 at 20:07test
first before theecho
(in case the filename you search for doesn't exist or doesn't have the right number of dots). I'll do an answer. – mikeserv Feb 26 '15 at 20:17