Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-31-2008, 09:59 AM   #1
Registered: May 2006
Posts: 64

Rep: Reputation: 15
how to get integer part from string in Shell Script

i wrote a script to get the product release. i am getting this correctly.
Now i need to get the major release which is 13 from 13m. How can i do this. please help me to get it work.

Thanks in advance.

Old 10-31-2008, 10:14 AM   #2
Senior Member
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Originally Posted by sunilvadranapu View Post
How can i do this.
Use regular expressions with "grep" or "sed".
Old 10-31-2008, 11:36 AM   #3
Registered: May 2007
Posts: 61

Rep: Reputation: 17
or, using awk and bash:
major_version=$( $version |awk -F '.' '{print $1}' )
Do you want the answer to be 13, or 13q? You could strip off the final character, like this:
integer=${major_version:0:${#major_version}-1 }
Old 10-31-2008, 11:39 AM   #4
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You can try also parameter substitution in two passes:
echo $release
The first substitution strips out the part of the string starting with a dot (.00.00.00), the second substitution removes all the alphabet characters.

Last edited by colucix; 10-31-2008 at 11:42 AM.
Old 11-02-2008, 04:51 AM   #5
LQ Newbie
Registered: Sep 2008
Location: Nigdi, Pradhikaran Pune
Distribution: debian, rpm
Posts: 7
Blog Entries: 2

Rep: Reputation: 0
One way you can do this is by using the cut and expr commands. You can obtain the
string as 13m from your script output for eg say- "13m.00.00.00" by piping the output to cut command. Then, further you can store the output in the variable "version" through the script as version=13m and use the expr command as #expr "$version" : '\(..\).' and the final output that you get is 13. I hope this will work for you.


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
converting string in integer in bash script dziadgba Linux - Newbie 5 08-31-2009 05:59 PM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
remove part of string in bash script crewblunts Programming 2 03-16-2006 05:54 PM
Simple bash script help, grabbing part of a string colabus Linux - Newbie 3 04-25-2005 09:42 AM
Shell-Script check if $xx is of type integer d-fens Programming 5 07-11-2003 08:38 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:20 AM.

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