convert a string / remove file name ending

For discussions about programming and projects not necessarily associated with Porteus.
port
Samurai
Samurai
Posts: 137
Joined: 18 Feb 2016, 09:25
Distribution: Linux porteus 3.2.2 KDE
Location: Spain

Re: convert a string / remove file name ending

Post#16 by port » 11 Mar 2016, 11:24

to get the version number only simply do that:

Code: Select all

V=${FILENAME%*.*.*}
V=${V#*[-_.]}
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

Code: Select all

V=${FILENAME%*.tar.*}
and if you want just .tar.gz replace it with

Code: Select all

V=${FILENAME%*.tar.gz}
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:

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"

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

Re: convert a string / remove file name ending

Post#17 by Rava » 11 Mar 2016, 14:21

Thanks for that, interesting approach...
Cheers!
Yours Rava

User avatar
brokenman
Site Admin
Site Admin
Posts: 6105
Joined: 27 Dec 2010, 03:50
Distribution: Porteus v4 all desktops
Location: Brazil

Re: convert a string / remove file name ending

Post#18 by brokenman » 11 Mar 2016, 18:35

to get the version number only simply do that:
V=${FILENAME%*.*.*}
V=${V#*[-_.]}
Using a common slackware file as an example:

FILENAME=openldap-client-2.4.42-x86_64-1.txz
V=${FILENAME%*.*}
echo $V
openldap-client-2.4.42-x86_64-1
V=${V#*[-_.]}
echo $V
client-2.4.42-x86_64-1

Verdict: No good for getting version if your file name has a name-with-a-dash
How do i become super user?
Wear your underpants on the outside and put on a cape.

port
Samurai
Samurai
Posts: 137
Joined: 18 Feb 2016, 09:25
Distribution: Linux porteus 3.2.2 KDE
Location: Spain

Re: convert a string / remove file name ending

Post#19 by port » 12 Mar 2016, 01:37

brokenman wrote:Verdict: No good for getting version if your file name has a name-with-a-dash
the filename pattern proposed was name-version-suffix where name would be any chars, version only numbers with dots and suffix two dotted names. That is, there's no version suffix like -x86_64-1 after version and before extension.

If this condtition is true or at least separators used in that version suffix are different and not used in name section (and providing version suffix is part of version) you can solve the name-with-a-dash problem (or generally name-with-a-separator problem) with this code:

Code: Select all

V=${V##*[-:]}
supposing sepator chars allowed in name are - and : while not allowed in version suffix (using for example _ and +). Note you must explicity avoid . in brackets because it's used in version number.

proof of concept:

Code: Select all

% F=openldap-client-2.4.42+x86_64+1.tar.xz
% V=${F%.*.*}
% V=${V##*[-:]}
% echo $V
2.4.42+x86_64+1
% F=openldap-client:bundle-2.4.42+x86_64+1.tar.xz
% V=${F%.*.*}
% V=${V##*[-:]}
% echo $V
2.4.42+x86_64+1
If the two preconditions are not satisfized then you need a more advanced program (awk, sed...) or advanced bash options (extglob) using this code:

Code: Select all

V=${V##+(+([[:alpha:]])[-:_.+])}
Now you can also cover all extension possibilities (all like tar.xz , .tgz , .tar.gz.crypt , .why.not.a.bit.longer.1more ) with the code:

Code: Select all

shopt -s extglob
V=${F%%+(.+([[:alnum:]]))}
V=${V##+(+([[:alpha:]])[-:_.+])}
this way you have no conflict with version or suffix separator chars:

Code: Select all

% shopt -s extglob
% F=openldap-client-2.4.42+x86_64+1.tar.xz
% V=${F%.*.*}
% echo ${V##+(+([[:alpha:]])[-:_.+])}
2.4.42+x86_64+1
% F=openldap-client:bundle-2.4.42+x86_64+1.tar.xz
% V=${F%.*.*}
% echo ${V##+(+([[:alpha:]])[-:_.+])}
2.4.42+x86_64+1
% F=openldap-client:bundle.new-2.4.42-x86_64-1.tar.xz
% V=${F%.*.*}
% echo ${V##+(+([[:alpha:]])[-:_.+])}
2.4.42+x86_64+1
% F=openldap-client.2.4.42-x86_64-1.tar.xz1
% V=${V##+(+([[:alpha:]])[-:_.+])}
% echo ${V##+(+([[:alpha:]])[-:_.+])}
2.4.42-x86_64-1
% F=openldap.client.2.4.42-x86_64-1.tgz
% V=${F%%+(.+([[:alnum:]]))}
% echo ${V##+(+([[:alpha:]])[-:_.+])}
2.4.42-x86_64-1
% F=openldap.client.2.4.42-x86_64-1.why.not.a.bit.longer.1more
% V=${F%%+(.+([[:alnum:]]))}
% echo ${V##+(+([[:alpha:]])[-:_.+])}
2.4.42-x86_64-1
% F=openldap-client_bundle+kiosk-2.4.42-x86_64-1.tar.gz.crypt.2
% V=${F%%+(.+([[:alnum:]]))}
% V=${V##+(+([[:alpha:]])[-:_.+])}
% echo $V
2.4.42-x86_64-1

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

Re: convert a string / remove file name ending

Post#20 by Rava » 17 Mar 2016, 15:44

Who would have thought that my small question would create such great and in depth answers.


Anyhow, what do you suggest in doing in filtering out the x86-64 or i586 or i486 or even noarch or such part, and so for one having that in a variable of its own (e.g. $ARCH), and also to only get the version number, as in

Code: Select all

% F=openldap-client-2.4.42+x86_64+1.tar.xz
% F=openldap-client:bundle-2.4.42+x86_64+1.tar.xz
% F=openldap-client:bundle.new-2.4.42-x86_64-1.tar.xz
% F=openldap-client:bundle.new-2.4.42-x86_64-1.tar.xz
% F=openldap-client:bundle.new-2.4.42-x86_64-1.tar.xz.crypt
and resulting in
V=2.4.42
ARCH=x86_64-1 (or just x86_64 ?)
and NAME=openldap-client:bundle.new

Me thinks that solution would be most efficient for any all-purpose pBuild scripts that might cross our path now, or in the future..
Cheers!
Yours Rava

Post Reply