The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Digits display Spoorthi16 UNIX for Dummies Questions & Answers 3 10-01-2007 09:26 PM
Only Digits as input namishtiwari UNIX for Dummies Questions & Answers 2 08-21-2007 07:23 AM
Perl code to differentiate numeric and non-numeric input Raynon Shell Programming and Scripting 11 08-04-2007 11:32 AM
Extract digits at end of string offirc Shell Programming and Scripting 6 11-20-2006 11:57 AM
How to cut last 10 digits off psarava Shell Programming and Scripting 4 08-29-2006 04:52 AM

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 06-30-2008
sollins sollins is offline
Registered User
  
 

Join Date: May 2008
Posts: 9
Retrieve the first 9 digits from the numeric value

Hi Buddy....

I want to retrieve the first 9 digits from the below numeric value.moreover i want to truncate prefix zero's from the retrived number

000000001200820060734


Retrived number should be passed to one more Shell Script.Pls guide me how to proceed.I'm new to Unix Shell Script...

Thanks
Soll
  #2 (permalink)  
Old 06-30-2008
chihung chihung is offline
Registered User
  
 

Join Date: Jun 2008
Location: Singapore
Posts: 48
Code:
num=000000001200820060734
echo $num | cut -c1-9 | sed -e 's/^[0]*//'
  #3 (permalink)  
Old 06-30-2008
flysen flysen is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 1
echo $num|sed -e 's/^[0]*//'|cut -b 1-9
  #4 (permalink)  
Old 06-30-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,285
All in one sed:
Code:
sed 's/^0*\(.\{9\}\).*/\1/g'
Your example works:
Code:
000000001200820060734
        ^^^^^^^^^

## If number of didigts in front changes, ie. if the last leading zero is not a prefix:
000000000200820060734
         ^^^^^^^^^
So this can go wrong and you might want to use the following, based on a fix number/position of digits:
Code:
echo "000000001200820060734"| cut -c8-17

Last edited by zaxxon; 06-30-2008 at 05:31 AM..
  #5 (permalink)  
Old 06-30-2008
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,540
Code:
# a=000000001200820060734
# echo ${a:0:9}
000000001
  #6 (permalink)  
Old 06-30-2008
radoulov's Avatar
radoulov radoulov is offline Forum Staff  
addict
  
 

Join Date: Jan 2007
Location: Варна, България / Milano, Italia
Posts: 2,879
What shell you are using?
With the latest bash you could write something like this:

Code:
% n=000000001200820060734
% printf -vn "%.9s" $((10#$n))
% echo $n
120082006
  #7 (permalink)  
Old 06-30-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,930
And another way
Code:
#!/bin/ksh93

n=000000001200820060734
n=${n/*(0)({9}(\d))*(\d)/\2}
print $n
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:35 AM.


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