1834804 Members
2783 Online
110070 Solutions
New Discussion

Leading Zeros

 
SOLVED
Go to solution
Dick Fischer
Occasional Advisor

Leading Zeros

Within a script, is there an easy way of checking for leading zeros or stripping leading zeros by using unix commands?

For the purpose of this discussion I am pulling a 6-character record count from file_1 and inserting the result into file_2 using the cut command. The record count has leading zeros. Is there an easy way to either get rid of the leading zeros, or use the cut or another command on file_2 to grab everything beyond the leading zeros? The result would end up in file_3. I tried translate (tr) but that got rid of all the zeros.
8 REPLIES 8
Rajeev  Shukla
Honored Contributor

Re: Leading Zeros

Yes you can very well do that, if you could tell me in more detail i'll let you know the format to do that or best is use awk command and cut the the fields which you want and put them in other file.
One more best thing would be to use sed command and replace all leading zeros with null.

Cheers
Rajeev
Bill Hassell
Honored Contributor

Re: Leading Zeros

Very easy with any POSIX shell such as /usr/bin/sh and /usr/bin/ksh. Use the typeset command to specify that variable as an integer. typeset also allows you to specify a fixed field for display, with or without leading zeros, etc. No need to play with awk or sed.


Bill Hassell, sysadmin
Sridhar Bhaskarla
Honored Contributor

Re: Leading Zeros

Hi,

Both have advantages and disadvantages.

It depends on the type of records that you pull out. typeset can really change the output if it is not used carefully. (for ex typeset a variable as integer and see what will happen if the input has characters). And sometimes it will leave no choice except to use awk or sed.

echo $WORD|sed 's/^0*//' will not change the type of your input records. But on the other hand, if you don't do substitution correctly, you will get the wrong results.

Make the choice based on the type of data that is passed.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Ricky B. Nino
Frequent Advisor

Re: Leading Zeros

to check leading zero/es...

In your SHELL Script you can enter...

1. grep 0*

2. check the value of $?.
if $?=0, then you have a leading 0 in your .

give it a try...
Opportunities expand for people willing to put time and effort into learning new skills.
RikTytgat
Honored Contributor
Solution

Re: Leading Zeros

Hi,

Try this:

$ typeset -i INTEGER
$ INTEGER=00005
$ echo $INTEGER
5
$

Is this what you want?

Ricky's solution will not work, since the grep will look for zeroes in a file named the value of the variable, and not in the value of the variable itself.

Bye,
Rik
Rodney Hills
Honored Contributor

Re: Leading Zeros

If you use variable substitution of ksh or posix-sh you can achieve the results.

x="000012"
y=${x##*0}
# y will be 12
if [ "$x" = "$y" ] ; then
echo "No leading zero"
else
echo "Yes leading zero"
fi
# will display "yes leading zero"

HTH

-- Rod Hills
There be dragons...
John Meissner
Esteemed Contributor

Re: Leading Zeros

run=1
while [ "$run" = "1" ]
do
z=$(cat file_1 | awk '{print $1}' | grep ^0)
if [ "$z" != "" ]
then
cat file_1 | sed 's/^0//g' > file_tmp
mv file_tmp file_1
elif [ "$z" = "" ]
then
run=0
fi
done
All paths lead to destiny
Dick Fischer
Occasional Advisor

Re: Leading Zeros

Thanks for all your help. Since I'm a novice at this stuff, some of the explanations were over my head, but the typeset command was the easiest one for me to grasp and utilyze. Points were therefore given on that basis.