LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

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

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


Hi,
i wrote a script to get the product release. i am getting this correctly.
version=13q.00.00.00;
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.

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

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

Rep: Reputation: 17
or, using awk and bash:
Code:
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:
Code:
integer=${major_version:0:${#major_version}-1 }
 
Old 10-31-2008, 12:39 PM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
You can try also parameter substitution in two passes:
Code:
version=13q.00.00.00
release=${version%%.*}
release=${release//[[:alpha:]]}
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 12:42 PM.
 
Old 11-02-2008, 05:51 AM   #5
rash31
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.
 
  


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


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration