convert a string / remove file name ending

For discussions about programming and projects not necessarily associated with Porteus.

convert a string / remove file name ending

Postby Rava » 19 Dec 2013, 09:49

For the updated x86-64 pBuild I need a way to convert a string, that holds the version number of FFX and it's original file endings
Code: Select all
26.0.tar.bz2

into this:
Code: Select all
26.0


The variable is named / refereed to as ${latest_version}

The only thing that's always the same is the ending, .tar.bz2, which should be cut of, so that the string only holds it's version number, here: 26.0, could also be 26.0.1 or such...

For some reason currently my brain is unable to come up with such a small task, could anyone of you help me out on that?
Cheers!
Yours Rava
User avatar
Rava
Contributor
Contributor
 
Posts: 1280
Joined: 11 Jan 2011, 03:46
Location: Germany
Distribution: Porteus 3.1.0 x86-64 XFCe

Re: convert a string / remove file name ending

Postby Quax » 19 Dec 2013, 17:30

Code: Select all
latest_version=$(echo "26.0.tar.bz2" | sed 's/\.tar\.bz2$//')

Regards,

Quax
Erfahrung bedeutet gar nichts - man kann Dinge auch 15 Jahre lang falsch machen...

Experience means nothing - one could have done things wrong for the last 15 years...
User avatar
Quax
Full of knowledge
Full of knowledge
 
Posts: 21
Joined: 29 Dec 2010, 22:28
Location: Muelheim an der Ruhr, Germany
Distribution: FluxFlux (ARM + x86), Slax7

Re: convert a string / remove file name ending

Postby brokenman » 24 Jan 2014, 01:11

version=26.0.tar.bz2
latest=${version%.*.*}
echo $latest

Sorry for necro bumping. Two months only, the corpse is still warm.
How do i become super user?
Wear your underpants on the outside and put on a cape.
brokenman
Site Admin
Site Admin
 
Posts: 5362
Joined: 27 Dec 2010, 04:50
Location: Brazil
Distribution: Porteus v3.2rcX all desktops

Re: convert a string / remove file name ending

Postby Rava » 01 Feb 2014, 19:58

brokenman wrote:version=26.0.tar.bz2
latest=${version%.*.*}
echo $latest

Sorry for necro bumping. Two months only, the corpse is still warm.


It is indeed very warm, since was not able to work on the script since then, but I still appreciate the suggestion.

Will in a few days or weeks give you the new version...
Cheers!
Yours Rava
User avatar
Rava
Contributor
Contributor
 
Posts: 1280
Joined: 11 Jan 2011, 03:46
Location: Germany
Distribution: Porteus 3.1.0 x86-64 XFCe

Re: convert a string / remove file name ending

Postby aus9 » 26 Jan 2016, 00:27

I know this is an old thread

try this
Code: Select all
cat file
26.0.tar.bz2
 
guest ~ $ sed -r 's/.{8}$//' file
26.0
aus9
 

Re: convert a string / remove file name ending

Postby Ed_P » 26 Jan 2016, 01:04

aus9 wrote:I know this is an old thread

Maybe you should lock it to prevent others from reactivating it. :oops:

But until you do, this approach works for me:
Code: Select all
FILENM=26.0.1.tar.bz2
LATEST_VERSION=${FILENM:0:-8}
echo $LATEST_VERSION


8)

You might also mark it as [Solved]. :wink:
Last edited by Ed_P on 26 Jan 2016, 04:29, edited 2 times in total.
Ed
User avatar
Ed_P
Contributor
Contributor
 
Posts: 2794
Joined: 06 Feb 2013, 23:12
Location: Western NY, USA
Distribution: Cinnamon 3.2.* 64-bit ISO

Re: convert a string / remove file name ending

Postby brokenman » 26 Jan 2016, 01:42

FILENM=26.0.1.tar.bz2
VERSION=${FILENM:0:-8}
echo $VERSION

Neither of your solutions will work if your file name is (for example):
FILENM=mypackage-26.0.1.tar.xz
A better expansion principle to use is: echo ${FILENM%.*.*}
Here we are not counting 8 characters backwards, but counting dot separated fields.

Maybe you should lock it to prevent others from reactivating it.

Lock every old thread? I will gladly give you the job if you want it. I have changed my mind on necro bumping in some cases. If the information is still relevant then why not add more useful information? I'm not exactly sure why people have a deep rooted hate for necro bumping.

PS: Stay on topic please :wink:
How do i become super user?
Wear your underpants on the outside and put on a cape.
brokenman
Site Admin
Site Admin
 
Posts: 5362
Joined: 27 Dec 2010, 04:50
Location: Brazil
Distribution: Porteus v3.2rcX all desktops

Re: convert a string / remove file name ending

Postby aus9 » 26 Jan 2016, 02:23

Neither of your solutions will work if your file name is (for example):
FILENM=mypackage-26.0.1.tar.xz


but to be anal that is not the original question so please!

Here is a possible solution to the new "improved" changed file contents

Code: Select all
cat file
mypackage-26.0.1.tar.xz

sed -r 's/.{10}//;s/.{7}$//' file
26.0.1


Ok its longer than yours ....not that I am boasting but I can count
aus9
 

Re: convert a string / remove file name ending

Postby Ed_P » 26 Jan 2016, 04:26

brokenman wrote:
FILENM=26.0.1.tar.bz2
VERSION=${FILENM:0:-8}
echo $VERSION

Neither of your solutions will work if your file name is (for example):
FILENM=mypackage-26.0.1.tar.xz
A better expansion principle to use is: echo ${FILENM%.*.*}
Here we are not counting 8 characters backwards, but counting dot separated fields.

But that is not what was stated by the OP and misses the 26.0.1 example he stated.
Rava wrote:For the updated x86-64 pBuild I need a way to convert a string, that holds the version number of FFX and it's original file endings
Code: Select all
26.0.tar.bz2

into this:
Code: Select all
26.0


The variable is named / refereed to as ${latest_version}

The only thing that's always the same is the ending, .tar.bz2, which should be cut of, so that the string only holds it's version number, here: 26.0, could also be 26.0.1 or such...


brokenman wrote:
Maybe you should lock it to prevent others from reactivating it.

Lock every old thread? I will gladly give you the job if you want it.

hmmmm we'll talk.

brokenman wrote:I have changed my mind on necro bumping in some cases.

Not familiar with that term.

brokenman wrote:If the information is still relevant then why not add more useful information? I'm not exactly sure why people have a deep rooted hate for necro bumping.

Well, a. usually the OP is no longer interested in the subject and b. may no longer be even interested in the forum, and c. it doesn't cost any more to start a new thread.

brokenman wrote:PS: Stay on topic please :wink:

:pardon: I thought we were. Late, but on topic.
Ed
User avatar
Ed_P
Contributor
Contributor
 
Posts: 2794
Joined: 06 Feb 2013, 23:12
Location: Western NY, USA
Distribution: Cinnamon 3.2.* 64-bit ISO

Re: convert a string / remove file name ending

Postby Bogomips » 26 Jan 2016, 19:06

My tuppence. As brokenman says
Code: Select all
guest@porteus:~$ f=26.0.1.tar.bz2
# Ending always .tar.bz2
guest@porteus:~$ v=${f%.tar.bz2}
guest@porteus:~$ echo $v
26.0.1
Or am I missing something here?
Linux porteus 4.4.0-porteus #3 SMP PREEMPT Sat Jan 23 07:01:55 UTC 2016 i686 AMD Sempron(tm) 140 Processor AuthenticAMD GNU/Linux
NVIDIA Corporation C61 [GeForce 6150SE nForce 430] (rev a2) MemTotal: 901760 kB MemFree: 66752 kB
Bogomips
Full of knowledge
Full of knowledge
 
Posts: 2293
Joined: 25 Jun 2014, 16:21
Location: London
Distribution: 3.2.2 Cinnamon & KDE5

Re: convert a string / remove file name ending

Postby brokenman » 26 Jan 2016, 21:10

v=${f%.tar.bz2}

Perfect.

Well, a. usually the OP is no longer interested in the subject and b. may no longer be even interested in the forum

From the point of view of someone else with the same problem looking for answers ... they wouldn't care if the OP suffered some tragically fatal accident with a gerbal. If someone can post the answer for them even after the OP has deserted the thread then, awesome.

and c. it doesn't cost any more to start a new thread

Can't say how much I hate it when I find a thread with the same problem that I am researching, with no real answers, and I have to go searching another tab. Please please, necro bump with answers that are still valid.

necro bump = resurrect and old thread.
How do i become super user?
Wear your underpants on the outside and put on a cape.
brokenman
Site Admin
Site Admin
 
Posts: 5362
Joined: 27 Dec 2010, 04:50
Location: Brazil
Distribution: Porteus v3.2rcX all desktops

Re: convert a string / remove file name ending

Postby aus9 » 26 Jan 2016, 23:51

Or am I missing something here?


Yes brokenman would also like to see a result for
FILENM=mypackage-26.0.1.tar.xz

I have no idea how to explain necro maybe he just means bumping and most forum users understand bumping or bump to get a question back to top of active topics / new posts. necro is too close to another word that I can't name here.
aus9
 

Re: convert a string / remove file name ending

Postby Rava » 02 Feb 2016, 19:25

Late reply is late, but thanks folks nonetheless.

Code: Select all
$ FILENM=mypackage-26.0.1.tar.xz
$ echo ${FILENM%.tar.*}
mypackage-26.0.1
$ FILENM=mypackage-26.0.1.tar.bz2
$ echo ${FILENM%.tar.*}
mypackage-26.0.1
$ FILENM=mypackage-26.0.1.tar.whatever
$ echo ${FILENM%.tar.*}
mypackage-26.0.1
Works for me. :)

It even works with the unlikely name of
Code: Select all
$ FILENM=my.tar.package-26.0.1.tar.gz
$ echo ${FILENM%.tar.*}
my.tar.package-26.0.1
Cheers!
Yours Rava
User avatar
Rava
Contributor
Contributor
 
Posts: 1280
Joined: 11 Jan 2011, 03:46
Location: Germany
Distribution: Porteus 3.1.0 x86-64 XFCe

Re: convert a string / remove file name ending

Postby aus9 » 03 Feb 2016, 00:27

@ Rava
I accidently marked this post as solved and then read your first post.

Your first post seems to be asking for a command etc to get to just a version number which is not what you now seem happy with in the last reply?

Code: Select all
FILENM=mypackage-26.0.1.tar.xz
echo $FILENM | sed -r 's/.{10}//;s/.{7}$//'
26.0.1

FILENM=my.tar.package-26.0.1.tar.gz
echo $FILENM | sed -r 's/.{15}//;s/.{7}$//'
26.0.1


I like it as its just a counting method. Now lets see if the others respond?
aus9
 

Re: convert a string / remove file name ending

Postby Rava » 06 Feb 2016, 07:55

aus9 wrote:Your first post seems to be asking for a command etc to get to just a version number which is not what you now seem happy with in the last reply?

I also have forgotten what I wrote in my initial post, so yeah, the issue is still unsolved.

Me thinks, anything but numbers and stop (aka ".") in between them can be masked as some kind of [a-z][A-Z] or such... I want it kind of foolproof, best so that it can be used for more than just this variant here, aka for firefox or palemoon modules, but for any modules that have the version number in a given / downloaded name and that the pBuild coder wants to import into a string so that it can be reused in the final version number.

[Like how I ATM do it, every time manually, with my palemoon module: It has the version number of palemoon, but also the version number of the Flash Plugin, but the silly thing with the flash plugin, if you download that, you never get a download that has the Flash version in the file name, but the version number is to be found on the adobe web page you download if from when you do it manually, and can be extracted by analysing the web page.
Of course, if Adobe changes that very coding of the web page, the extraction of the version number could fail.
So, in both cases, the version number for palemoon, and for Flash should always be checked if they are indeed non-empty, and if they are made out of numbers.
Cheers!
Yours Rava
User avatar
Rava
Contributor
Contributor
 
Posts: 1280
Joined: 11 Jan 2011, 03:46
Location: Germany
Distribution: Porteus 3.1.0 x86-64 XFCe

Next

Return to Programming



Who is online

Users browsing this forum: No registered users and 2 guests