LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-08-2010, 07:37 AM   #1
nadinnne
LQ Newbie
 
Registered: Jun 2010
Posts: 6

Rep: Reputation: 0
Question Convert string to number without 0 in the beginning


Hello!
I want to convert strings like 002, 049, 050, 100 to numbers: 2, 49, 50, 100.
How can I do this?
I tried to use:
Code:
`printf "%5d" $i`
, but it doesnt work , it convert:
002=>2
014=>12
049=>0
 
Old 06-08-2010, 07:43 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Which language are you using? Bash, for example, will remove the 0 without asking.
 
Old 06-08-2010, 07:47 AM   #3
nadinnne
LQ Newbie
 
Registered: Jun 2010
Posts: 6

Original Poster
Rep: Reputation: 0
I use Bash
 
Old 06-08-2010, 07:52 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi again,

If you print the variable (echo $i) are the leading zero's still there?

If so: Do you have to use printf?

This: ${i##+(0)} should work.

Hope this helps.
 
Old 06-08-2010, 08:00 AM   #5
nadinnne
LQ Newbie
 
Registered: Jun 2010
Posts: 6

Original Poster
Rep: Reputation: 0
It didn't help
My sample code is:
Code:
#!/bin/bash

test_field="FLD_002"
converted=${test_field/FLD_}
echo $converted
echo ${converted##+(0)}
And it prints in console:
002
002
 
Old 06-08-2010, 08:01 AM   #6
rikijpn
Member
 
Registered: Jun 2007
Location: Japan
Distribution: Debian lenny, DSL, Solaris 10
Posts: 157

Rep: Reputation: 33
Quote:
Originally Posted by nadinnne View Post
Hello!
I want to convert strings like 002, 049, 050, 100 to numbers: 2, 49, 50, 100.
How can I do this?
I tried to use:
Code:
`printf "%5d" $i`
, but it doesnt work , it convert:
002=>2
014=>12
049=>0
Close man
Code:
`printf "%05d" $i`
You needed to put "05", not just "5". Actually, shouldn't it be 3? Whatever, just put a 0 after the %.
 
Old 06-08-2010, 08:07 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Bash treats integers that start with 0 as octal values. Then printf is translating them to decimal.

The ABSG section on the ${var##} parameter substitution includes an example function for stripping zeroes off of variables.

Edit: @druuna; your code requires enabling the extglob option first, as the example I just linked to shows.

Last edited by David the H.; 06-08-2010 at 08:11 AM.
 
Old 06-08-2010, 08:07 AM   #8
nadinnne
LQ Newbie
 
Registered: Jun 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by rikijpn View Post
Close man
Code:
`printf "%05d" $i`
You needed to put "05", not just "5". Actually, shouldn't it be 3? Whatever, just put a 0 after the %.
It just adds 0 in the beginning..
I don't know why, but works differently with 049 and 002:

Code:
#!/bin/bash

test_field="FLD_049"
converted=${test_field/FLD_}
echo $converted
echo ${converted##+(0)}
printf "%d" $converted
printf "\n"
This prints:
049
049
./test.sh: line 7: printf: 049: invalid number
0

But:
Code:
#!/bin/bash

test_field="FLD_002"
converted=${test_field/FLD_}
echo $converted
echo ${converted##+(0)}
printf "%d" $converted
printf "\n"
Prints:
002
002
2

If I ad 0:
Code:
#!/bin/bash

test_field="FLD_002"
converted=${test_field/FLD_}
echo $converted
echo ${converted##+(0)}
printf "%03d" $converted
printf "\n"
:
002
002
002
 
Old 06-08-2010, 08:09 AM   #9
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi again,

Have a look at the link David the H. posted (I was looking for it, but he beat me to it ).

Hope this helps.
 
Old 06-08-2010, 08:19 AM   #10
nadinnne
LQ Newbie
 
Registered: Jun 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by druuna View Post
Hi again,

Have a look at the link David the H. posted (I was looking for it, but he beat me to it ).

Hope this helps.
Ooo, it works like:
Code:
#!/bin/bash

test_field="FLD_002"
converted=${test_field/FLD_}
shopt -s extglob 
converted=${converted##+(0)}
shopt -u extglob  
echo $converted
Thank You!!!
 
Old 06-08-2010, 08:37 AM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Glad you got it. There's one minor warning that was pointed out in the ABSG code though. if var=0; then "${var##+(0)}" will result in an empty field. So you might want to run a quick test for that if you think it's likely to be a problem.
 
Old 06-08-2010, 09:27 AM   #12
nadinnne
LQ Newbie
 
Registered: Jun 2010
Posts: 6

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by David the H. View Post
Glad you got it. There's one minor warning that was pointed out in the ABSG code though. if var=0; then "${var##+(0)}" will result in an empty field. So you might want to run a quick test for that if you think it's likely to be a problem.
Yes, it returns an empty field. But in my case there will not be var=0, so it should work.

Thanks!
 
  


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] How to convert string to integer back to string C++ LazerPhreak Programming 1 03-13-2010 08:32 PM
display all text beginning at <string> daemon_14 Linux - General 4 11-11-2008 04:36 PM
!? element names beginning with number break javascript??? UMG:Chicken_Sop Programming 1 01-28-2008 12:12 AM
convert string into number, shell mystical dervish Programming 9 08-13-2007 04:32 AM
Shell Script that sorts data with number beginning on each line. sunksullen Programming 12 05-09-2007 04:35 PM


All times are GMT -5. The time now is 01:10 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration