Re: convert a string / remove file name ending
Posted: 11 Mar 2016, 11:24
to get the version number only simply do that:
assuming FILENAME is a variable containing file name, this works whatever combination of ending suffix while following the pattern .a.b (i,e .tar.gz .tar.bz .x.Z ...) and a prefix separated for version number with chars - _ or . (i.e myfile.19.2.3.tar.gz afile-2.4.bar.Z thefile_1.1.1-00010.x.y )
if you want to limit suffix to just .tar.* replace first line with
and if you want just .tar.gz replace it with
Also if you want more (or less) chars allowed to separate name prefix and version number, just include the desired chars between brackets, for example if you only want - and _ use this code:
and if you want the chars . - _ : + use
proof of concept:
Code: Select all
V=${FILENAME%*.*.*}
V=${V#*[-_.]}
if you want to limit suffix to just .tar.* replace first line with
Code: Select all
V=${FILENAME%*.tar.*}
Code: Select all
V=${FILENAME%*.tar.gz}
Code: Select all
V=${V#*[-_]}
and if you want the chars . - _ : + use
Code: Select all
V=${V#*[-_:+.]}
proof of concept:
Code: Select all
#!/bin/bash
echo -n "filename: "; read F
V=${F%*.*.*}
V=${V#*[-_:+.]}
echo "$F -> $V"