LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 01-19-2006, 01:31 AM   #1
Luc484
Member
 
Registered: Dec 2004
Distribution: Kubuntu
Posts: 62

Rep: Reputation: 15
[Solved] Getting a number out of a string in bash


Hi. I have the output of a command with grep which is string with a number inside. Is it possible to get that number somehow? For instance, I have this string:

Code:
Pages:         n
where n is an integer. Is it possible to get the n and to put it into another variable to use it with some arithmetic operators?
Thanks.

Last edited by Luc484; 01-19-2006 at 04:59 PM.
 
Old 01-19-2006, 08:24 AM   #2
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
from your requirement

Code:
echo "Pages:    10" | sed 's/[^0-9]//g'
suppress everything except digits from 0-9.

and in a variable
Code:
var=`echo "Pages:    10" | sed 's/[^0-9]//g'`
echo $var
Hope this helps.
 
Old 01-19-2006, 11:07 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
...and after that you can use the number $var (still being a string, as everything in bash scripts is a string) like thisin arithmics:
Code:
# ...
var2=$(($var + 1))
echo$var2
 
Old 01-19-2006, 04:59 PM   #4
Luc484
Member
 
Registered: Dec 2004
Distribution: Kubuntu
Posts: 62

Original Poster
Rep: Reputation: 15
Great, exactly what I needed. Thanks.
 
Old 01-19-2006, 11:14 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Alternatively, you could use:
awk '{print $2}'
instead of the sed stuff; just another option. Personally I find it easier to read.
 
Old 01-20-2006, 02:15 AM   #6
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
one more alternative with sed,

Code:
echo "Pages:    10" | sed 's/.* //g'
 
  


Reply



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
strip number from string (c code) alaios Programming 8 09-26-2005 12:47 AM
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM
macro to repeat a token/string n number of times saravkrish Programming 11 05-24-2005 01:31 AM
replace a string/number in a text file jpan Linux - General 3 10-22-2004 09:33 PM
BASH string expansions Barbarian Programming 2 11-21-2001 10:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:52 AM.

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