LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 06-01-2016, 02:47 AM   #1
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Rep: Reputation: 73
Question How to process different version string using sed/cut


Hello, I'm trying to get the major/minor version from a versions string, but can have different version strings.

For example:
Code:
version="124.0.0.56" 
 or 
version="123.0(123)"
Getting major its kind of easy:
Code:
echo $version | cut -d. -f1
but getting minor which would mean '56' or '123' is difficult or I haven't found a way to do it with a single sed.

I currently use this:
Code:
version="124.0.0.56"
echo "$version" | sed -n 's/\([0-9].*\)\.\([0-9].*\)\.\([0-9].*\)\.\([0-9].*\)/\4/p'

version="123.0(123)"
echo $version | sed -n 's/\([0-9].*\)\.\([0-9].*\)(\([0-9].*\))/\3/p'
Is there a way to unify this 2 sed's into one?
 
Old 06-01-2016, 02:59 AM   #2
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Original Poster
Rep: Reputation: 73
Sorry, actually there are 3 possible version strings:

Code:
version="124.0.0.56"
 or
version="121.0.0.0.31" 
 or 
version="123.0(123)"
 
Old 06-01-2016, 03:16 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
You can use (multiple) -e to run multiple tests on the same record. And yes you could probably scan all in one regex, but it'd be complex to test. Is the time that would be invested worth it ?.
 
1 members found this post helpful.
Old 06-01-2016, 04:46 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
I would split on non-digit characters:
Code:
V=( $(echo $version | sed -r 's/[^0-9]+/ /g') )
echo ${V[0]}
 
2 members found this post helpful.
Old 06-01-2016, 04:49 AM   #5
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
My thinking is that it is easier to replace all '.' and '()' chars with whitespace so that we get columns. The last column is the one we want.

Code:
echo "version=\"123.0(123)\"" | sed -r 's/(\.|\(|\))/ /g' | sed -r 's/("$| "$)//' | awk '{ print $NF }'
123

echo "version=\"124.0.0.56\"" | sed -r 's/(\.|\(|\))/ /g' | sed -r 's/("$| "$)//' | awk '{ print $NF }'
56

echo "version=\"121.0.0.0.31\"" | sed -r 's/(\.|\(|\))/ /g' | sed -r 's/("$| "$)//' | awk '{ print $NF }'
31
So, this:
Code:
sed -r 's/(\.|\(|\))/ /g' | sed -r 's/("$| "$)//' | awk '{ print $NF }'
works for all the test cases. Can probably be done better/more efficient.

Best regards,
HMW
 
1 members found this post helpful.
Old 06-01-2016, 04:51 AM   #6
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by pan64 View Post
I would split on non-digit characters:
Code:
V=( $(echo $version | sed -r 's/[^0-9]+/ /g') )
echo ${V[0]}
^This is great and much better than my approach.

Last edited by HMW; 06-01-2016 at 04:57 AM. Reason: It is MUCH better!
 
Old 06-01-2016, 04:58 AM   #7
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Original Poster
Rep: Reputation: 73
Yes, all of the ideas are good and thanks for that, but this seems to work:

Code:
$ echo "122.0.0.4" | sed -n -e 's/\([0-9].*\)\.\([0-9].*\)\.\([0-9].*\)\.\([0-9].*\)/\4/p' -e 's/\([0-9].*\)\.\([0-9].*\)(\([0-9].*\))/\3/p'
4
$ echo "122.0.0.0.4" | sed -n -e 's/\([0-9].*\)\.\([0-9].*\)\.\([0-9].*\)\.\([0-9].*\)/\4/p' -e 's/\([0-9].*\)\.\([0-9].*\)(\([0-9].*\))/\3/p'
4
$ echo "121.0(24)" | sed -n -e 's/\([0-9].*\)\.\([0-9].*\)\.\([0-9].*\)\.\([0-9].*\)/\4/p' -e 's/\([0-9].*\)\.\([0-9].*\)(\([0-9].*\))/\3/p'
24
Thanks again guys!
 
Old 06-01-2016, 05:03 AM   #8
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Actually, I don't know why I didn't combine my two seds into one, like this:
Code:
echo "version="123.0(123)"" | sed -r 's/(\.|\(|\)|")/ /g' | awk '{ print $NF }'
123
So, this is a lot better:
Code:
sed -r 's/(\.|\(|\)|")/ /g' | awk '{ print $NF }'


But Pan64's approach is still the best!
HMW

Last edited by HMW; 06-01-2016 at 05:04 AM.
 
1 members found this post helpful.
Old 06-01-2016, 05:35 AM   #9
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Original Poster
Rep: Reputation: 73
Quote:
Originally Posted by HMW View Post
Actually, I don't know why I didn't combine my two seds into one, like this:
Code:
echo "version="123.0(123)"" | sed -r 's/(\.|\(|\)|")/ /g' | awk '{ print $NF }'
123
So, this is a lot better:
Code:
sed -r 's/(\.|\(|\)|")/ /g' | awk '{ print $NF }'


But Pan64's approach is still the best!
HMW
Yep, you got a point.
 
Old 06-01-2016, 05:52 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
Quote:
Originally Posted by HMW View Post
Code:
sed -r 's/(\.|\(|\)|")/ /g' | awk '{ print $NF }'
If awk, do not add sed:
Code:
awk ' BEGIN { FS="[^0-9]*" } { print $NF }'
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Sed/awk/cut to pull a repeating string out of a longer string StupidNewbie Programming 4 09-13-2018 03:41 AM
[SOLVED] how to add and delete a string in front of a string by sed? Ben Wang Linux - Newbie 6 05-16-2015 06:23 PM
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 11:05 AM
sed: how to change MAC address string with sed cold Linux - Software 5 08-02-2010 07:43 AM
Trying to change String using sed with a string \/home\/user\/Desktop icecoolcorey Programming 10 06-12-2008 11:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 04:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration