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
num=000000001200820060734
echo $num | cut -c1-9 | sed -e 's/^[0]*//'
flysen
June 30, 2008, 4:04am
3
echo $num|sed -e 's/^[0]*//'|cut -b 1-9
zaxxon
June 30, 2008, 4:18am
4
All in one sed:
sed 's/^0*\(.\{9\}\).*/\1/g'
Your example works:
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:
echo "000000001200820060734"| cut -c8-17
# a=000000001200820060734
# echo ${a:0:9}
000000001
What shell you are using?
With the latest bash you could write something like this:
% n=000000001200820060734
% printf -vn "%.9s" $((10#$n))
% echo $n
120082006
And another way
#!/bin/ksh93
n=000000001200820060734
n=${n/*(0)({9}(\d))*(\d)/\2}
print $n
And Z-Shell:
zsh-4.3.4% typeset -i n=000000001200820060734
zsh-4.3.4% print $n[1,9]
120082006
another way
# n=100400001200820060734
# read -n 9 a <<<$n
# echo $a
100400001
cuitao
July 1, 2008, 2:23am
10
another option:
typeset -L9 a
a=<the number>
This is nice, I suppose it's ksh (ksh93?) feature (also supported by zsh)
zsh-4.3.4% typeset -L9 n=000000001200820060734
zsh-4.3.4% print $n
000000001
Given the OP requirement it should be (assuming integer value, of course):
zsh-4.3.4% typeset -iL9 n=000000001200820060734
zsh-4.3.4% print $n
120082006