The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell script for converting file contents into CSV rish_max Shell Programming and Scripting 5 02-06-2009 02:51 AM
converting the data type in unix shell script kmanivan82 Shell Programming and Scripting 5 10-18-2008 02:07 PM
Converting Shell Script to HTML davwel Shell Programming and Scripting 3 10-25-2007 11:25 PM
Converting Stirngs to number raman1605 Shell Programming and Scripting 2 10-10-2007 06:34 AM
Converting Shell script to Dos batch files darwinkna Shell Programming and Scripting 1 05-12-2006 12:01 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-27-2009
amitanshu.verma amitanshu.verma is offline
Registered User
  
 

Join Date: May 2009
Posts: 2
Problem in converting number in shell script

Hi All,

I am writing a shell script in which I want to convert a number like :

Suppose the number is "98487657" and we have to convert it to "98000000", what I want to do is to retain first 2 digits and convert all remaining digits to "0".

Number could be of any length (length > 2).



Thanks in advance
  #2 (permalink)  
Old 05-27-2009
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,342
With awk:


Code:
echo '98487657' | awk '{printf("%d\n", substr($0,1,2) * 10^(length-2))}'

Regards
  #3 (permalink)  
Old 05-27-2009
amitanshu.verma amitanshu.verma is offline
Registered User
  
 

Join Date: May 2009
Posts: 2
Above command is working perfectly!!!

Thanks once again
  #4 (permalink)  
Old 05-27-2009
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,937
No need to call an external command. The following works for either bash or ksh93

Code:
$ NO=98487655
$ echo $NO
98487655
$ printf "%d\n" $((NO - ${NO:2}))
98000000

  #5 (permalink)  
Old 05-27-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,365

In any POSIX shell:


Code:
no=98487655
right=${no#??}
printf "%d%0${#right}d\n" "${no%"$right"}" 0

Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:26 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0