Awk help required for formatting digits.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk help required for formatting digits.
# 1  
Old 02-21-2012
Awk help required for formatting digits.

Hi experts,

I have two values in the file :

For example :
partcamt = 72.90
partdamt=27.9

I need to convert these values into 6 digits and ignore the "." sign so that the desired desired output is :
total value= 0072000027900

Currently I am using the following code :
Code:
  partCamount=substr($0,175,6)
   partDamount=substr($0,187,5)
   partCamount1=sprintf("%06d",partCamount)
   partDamount1=sprintf("%06d",partDamount)

So I am getting the output as 0000720000270.

Can you please help.
# 2  
Old 02-21-2012
Perhaps printf can help?

i.e.
Code:
$ awk 'BEGIN { printf "%06d\n", 27.9*10^2 }' #err, x100, I suppose!
002790

Your approach seems very ad-hoc. Could you show some representative input data?
# 3  
Old 02-21-2012
Thanks Scott.

Here is the input file :

Code:
0116WORCE   STD       KAHN                YETTA     UF1929042101453                         112.00      58.40E

0115NORFO   STD       CHETKAUSKAS         LONGIE       M1922060502021                           0.00       0.00E

0115NORFO   STD       CHETKAUSKAS         FLORENCE     F1917122102021                           0.00       0.00E

0104MIDDL   STD       DRISCOLL            TERESA      CF1928122101867                         112.00      58.40E

Now I need to consider the last 2 records in the file(ignoring E) and merge them together (removing the decimal point) and total length would 12 characters each.
Each field would have total 6 characters after the decimal point is removed.
# 4  
Old 02-21-2012
I don't think awk will care about the "E":
Code:
$ cat file1
0116WORCE   STD       KAHN                YETTA     UF1929042101453                         112.00      58.40E
0115NORFO   STD       CHETKAUSKAS         LONGIE       M1922060502021                           0.00       0.00E
0115NORFO   STD       CHETKAUSKAS         FLORENCE     F1917122102021                           0.00       0.00E
0104MIDDL   STD       DRISCOLL            TERESA      CF1928122101867                         112.00      58.40E
$ awk '{printf "%06d%06d\n", $(NF-1)*100, $NF*100}' file1
011200005840
000000000000
000000000000
011200005840

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed / awk script to delete the two digits from first 3 digits

Hi All , I am having an input file as stated below 5728 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r03_q_reg_20_/Q 011 611 U_TOP_LOGIC/U_CM0P/core/u_cortexm0plus/u_top/u_sys/u_core/r04_q_reg_20_/Q 011 3486... (4 Replies)
Discussion started by: kshitij
4 Replies

2. UNIX for Beginners Questions & Answers

Using sed or awk to replace digits in files

Hello; I am not good at file and stream editing. I need to replace a few digits in two files. The lines in files looks like this: Line in the first file, /dw300/data/obe/2019273.L800JR.1909.273 Line in second file, 1|2019273.L800JR.1909.273 I will write a function to connect to... (7 Replies)
Discussion started by: duke0001
7 Replies

3. Shell Programming and Scripting

Formatting required with the output

i have a o/p from find command that needs to be formatted currently when i'm running find . -name "v.info" it is giving below o/p /o/a/b/c/v.info /o/a/b/c/d/v.info /o/aa/bb/cc/v.info /o/aa/bb/cc/dd/v.info my requirement is if v.info is coming under sub-directories it shul be... (15 Replies)
Discussion started by: nikhil jain
15 Replies

4. Shell Programming and Scripting

awk changes to cut number of digits

HCPM1ONDB00014800011800000589009211201 L201307022013070228AUD 00000000031. 000965105800000000000000000000000 MOBITV KEYA ... (4 Replies)
Discussion started by: mirwasim
4 Replies

5. Shell Programming and Scripting

Find filenames with three digits and add zeros to make five digits

Hello all! I've looked all over the internet and this site and have come up a loss with an easy way to make a bash script to do what I want to do. I have a file with a naming convention as follows: 2012-01-18 string of words here 123.jpg 2012-01-18 string of words here 1234.jpg 2012-01-18... (2 Replies)
Discussion started by: Buzzman25
2 Replies

6. Shell Programming and Scripting

awk concat lines between 2 sequent digits

I would like to print string between two sequent digits and concatenate it into one single line. input.txt 99 cord, rope, strand, twine, twist, 100 strand, twine, twist, cord, rope 101 strand, twine, twist, twine, twist, cord, rope 105 cord, rope ,twi ... (8 Replies)
Discussion started by: sdf
8 Replies

7. Shell Programming and Scripting

awk search between 2 digits a string

I would like to search between two a string. I thought this would be easy. The is always at the beginning of a line. The code: gawk '/^/{d=$1},/searchstring/,/^(d+1)/' or gawk '/^/,/searchstring/,/^/' did not return the desired result. inputfile.txt 999 some text searchstring some... (6 Replies)
Discussion started by: sdf
6 Replies

8. Shell Programming and Scripting

Help required with formatting in scripting

Hi Friends, I need to write a script which reads the file and prints them horizontally. For example, the file contains something like x1 x2 x3 x4 x5 my script reads this file as "for i in `cat filename`", but I need an output something like "config file = x1.ccfg,... (3 Replies)
Discussion started by: dineeshkg
3 Replies

9. UNIX Desktop Questions & Answers

Help required with formatting

I would appreciate any help (sed / awk / perl) on the following question. I have the file in the following format. Note that the records are separated by the line that starts with the word "TRACE".I want the 5th and 6th values on the line starting with "TRACE" to be repeated down the file until the... (3 Replies)
Discussion started by: digipak
3 Replies

10. Shell Programming and Scripting

Formatting digits

I want to check the argument in KSH. If the user type in the prompt 'find 3' it will format 3 to 003 to match the data in the text file. Same as with 10 to 010. Always begins with 0. eg. >find 3 Output: 003 >find 30 Output: 030 (7 Replies)
Discussion started by: harry0013
7 Replies
Login or Register to Ask a Question