Sponsored Content
Top Forums Shell Programming and Scripting help in retaining leading zero Post 302547584 by udelalv on Tuesday 16th of August 2011 05:25:33 AM
Old 08-16-2011
Question help in retaining leading zero

Hello.

I'm trying to add multiple numbers with varying length and with leading zeroes in it. However, I'm getting the sum (totalHashAccountNumber) without the leading zeroes in it. How do I retain the leading zeroes?

Please pardon the lengthy code.. I'm getting the hash account number from 2 different fields and do translation when there are non-numeric characters in the account numbers.

Code:
#---------------------------------------------------------------#
# compute for the total hash account number                     #
#---------------------------------------------------------------#
typeset -i totalHashAccountNumber=0
while read line
do
  #rec_type=$(expr substr "$line" 1 1)
  rec_type=`echo "$line" | cut -c1-1`
  if [[ "$rec_type" == 'P' ]]; then
    #IBAN=$(expr substr "$line" 1021 34)
    IBAN=`echo "$line" | cut -c1021-1054`

    if [[ "$IBAN" == '' ]]; then
      #accntNum=$(expr substr "$line" 268 18)
      accntNum=`echo "$line" | cut -c268-285`
      hashAccountNum=$accntNum
    else
      hashAccountNum=$IBAN
    fi # end - if IBAN is null

      # check if hashAccountNum is all numeric
      if [[ $hashAccountNum == +([0-9]) ]]; then
        totalHashAccountNumber=$totalHashAccountNumber+$hashAccountNum
      else # translate non-numeric characters from hashAccountNum
        tempHashAmt=`echo $hashAccountNum | tr 'AKUBLVCMWDNXEOYFPZGQHRISJT' '00011122233344455566778899'`

        if [[ $tempHashAmt == +([0-9]) ]]; then
          totalHashAccountNumber=$totalHashAccountNumber+$tempHashAmt
        else
          i=1
          tempHash=""
          while (( i <= ${#tempHashAmt} ))
          do
            char=$(expr substr "$tempHashAmt" $i 1)
            case "$char" in
              [0-9] ) ;;
              * ) char="0"
            esac
            hashNum=$tempHash$char
            tempHash=$hashNum
            echo "tempHash: "$tempHash
            (( i += 1 ))
          done
          totalHashAccountNumber=$totalHashAccountNumber+$tempHash
        fi # end - second check if numeric
      fi # end - hashAccountNum is numeric   
  fi # end - get rec_type
done < $stagedir/$1

These are the account numbers I'm adding:

hashAccountNumber: 06511008546
hashAccountNumber: 00258088435
hashAccountNumber: 00000091580
hashAccountNumber: 01002447931

This is the sum that I'm getting: totalHashAccountNumber: 7771636492
I'm expecting 07771636492.

Thank you in advance.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

variable not retaining value

Bourne shell Solaris I'm trying to set a flag to perform an action only when data is found. So I initialize the flag with: X=0 Then I read the data: if ; then while read a b c do X=1 done < ${inputFile} fi The problem is that X will be set to 1 inside the while loop but when... (5 Replies)
Discussion started by: gillbates
5 Replies

2. Shell Programming and Scripting

Retaining tar.gz after gunzip

gunzip fnam.tar.gz After this command execution... .gz file no longer exists... and only fnam.tar is present. Is it possible to retain the tar.gz file after after using the above command thx in advance. (4 Replies)
Discussion started by: devs
4 Replies

3. Linux

retaining the evironment varaibles...

Hi all, I wrote a shell script in which one of the if condition i tried to set few variables and exported them...and when i am out of if condition i wish to run one of my script which uses those variables exported. But i found that when i am out of the if statement those variables... (5 Replies)
Discussion started by: priya444
5 Replies

4. UNIX for Dummies Questions & Answers

Retaining Spaces within a word

Hi Experts, I have a 2 GB flat file which have unicode field, some of them are blanks and its size is 4000 character. In the existing system SED command removes the spaces. Because of this field itself....it is taking almost three days to complete the file processing. I removed sed and... (0 Replies)
Discussion started by: RcR
0 Replies

5. Shell Programming and Scripting

retaining file path

hi all, Is there any way to retain file path? echo "Please enter your old filename" read a #user input : /blah/blah1 echo "please enter the new filename" read b #user input : dumb mv $a $b What i would like to do is for the user to enter just the "filename" for the new filename... (2 Replies)
Discussion started by: c00kie88
2 Replies

6. Shell Programming and Scripting

Retaining spaces between words

Retaining Spaces within a word -------------------------------------------------------------------------------- Hi Experts, I have a 2 GB flat file which have unicode field, some of them are blanks and its size is 4000 character. In the existing system SED command removes the spaces.... (7 Replies)
Discussion started by: RcR
7 Replies

7. Shell Programming and Scripting

Retaining default OFS

Hi, I wat to remove the first two columns of the ls -l command: $ ls -l | awk '{ $1="";$2="";print;}' The only problem is now the columns of the output is space separated instead of the separators the ls -l originally throws. Can someone tell how to preserve the default OFS of an ls -l... (3 Replies)
Discussion started by: amicon007
3 Replies

8. Shell Programming and Scripting

Retaining whitespaces...

There's an input file(input.txt) which has the following details : CBA BA <Please note the second record has a LEADING WHITESPACE which is VALID> I am using the following code to read the content of the said file line by line: while read p ; do echo "$p" done < input.txt This is the... (1 Reply)
Discussion started by: kumarjt
1 Replies

9. Shell Programming and Scripting

Retaining latest file

Hi All, I have a requirement where there are 2 files saved on unix directory with names anil_111 and anil_222. I just have to retain the latest file and delete the old file from the directory. Please help me with the shell script to perform this. Thanks, Anil (7 Replies)
Discussion started by: anil029
7 Replies

10. Shell Programming and Scripting

Retaining value outside loop

Hi, I m new to shell scripting. I did some research and understand that unix treats while and other loops as new shell and hence the variable loose its value outside of the loop. I found solution for integer variable but in mycase this is a string variable. here variable loc is a... (6 Replies)
Discussion started by: knowyrtech
6 Replies
expr(1) 						      General Commands Manual							   expr(1)

Name
       expr - evaluate expressions

Syntax
       expr arg...

Description
       The arguments are taken as an expression.  After evaluation, the result is written on the standard output.  Each token of the expression is
       a separate argument.

       The operators and keywords are listed below.  The list is in order of increasing precedence, with equal precedence operators grouped.

       expr | expr	   Yields the first expr if it is neither null nor 0.  Otherwise yields the second expr.

       expr & expr	   Yields the first expr if neither expr is null or 0.	Otherwise yields 0.

       expr relop expr	   The relop is one of < <= = != >= > and yields 1 if the indicated comparison is true, '0' if false.  The  comparison	is
			   numeric if both expr are integers, otherwise lexicographic.

       expr + expr
	    expr - expr
			   Yields addition or subtraction of the arguments.

       expr * expr
	    expr / expr
	    expr % expr
			   Yields multiplication, division, or remainder of the arguments.

       expr : expr	   The	matching  operator compares the string first argument with the regular expression second argument; regular expres-
			   sion syntax is the same as that of The (...) pattern symbols can be used to select a portion of the  first  argument.
			   Otherwise, the matching operator yields the number of characters matched ('0' on failure).

       ( expr ) 	   parentheses for grouping.

Examples
       The first example adds 1 to the Shell variable a:
       a=`expr $a + 1`
       The second example finds the file name part (least significant part) of the pathname stored in variable a,
       expr $a : '.*/(.*)' '|' $a
       Note the quoted Shell metacharacters.

Diagnostics
       The command returns the following exit codes:

       0    The expression is neither null nor '0'.

       1    The expression is null or '0'.

       2    The expression is invalid.

See Also
       ed(1), sh(1), test(1)

																	   expr(1)
All times are GMT -4. The time now is 05:51 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy