Zero padding a Number before and after a decimal place


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Zero padding a Number before and after a decimal place
# 1  
Old 10-26-2017
Zero padding a Number before and after a decimal place

Hi I was hoping someone could help me with a sed script I am trying to write? I am on a Mac running ElCapitan
I have some text that I have converted from a pdf that I want to format into an xml file.
In the file I have managed to delete all the text I do not need. The text I have left is Identified with an x and y coordinate at the beginning of the line, for its position on the page from the bottom left where x is 0 & y is 0. This is fortunate for me as everything with the same x coordinate is under the same column and every thing with the same y coordinate is on the same line.
My problem is I need to do a sort on these lines to get them in an order that I can use. I also need to append the y coordinates on page 2 onward with an 02 to 99 to denote the page it is on and to make sure when I do the sort items from page 2 onward stay in the correct page order.
This is an example of the text I have to work with
Code:
227.25 214.100 PLACEHOLDER (Qty) Tj
409. 214.100 PLACEHOLDER (Ink) Tj
19.8999 214.149 PLACEHOLDER (Prt) Tj
250.100 214.100 PLACEHOLDER (Hours) Tj

I wanted to create leading and trailing zeros so that the decimal place will continue to be in the same position from the left I was thinking that 6 places before the decimal point and after would probably work fine and leave some wiggle room if I come across something unexpected. Hopefully the text after would appear as
Code:
000227.250000 000214.100000 PLACEHOLDER (Qty) Tj
000409.000000 000214.100000 PLACEHOLDER (Ink) Tj
000019.899900 000214.149000 PLACEHOLDER (Prt) Tj
000250.100000 000214.100000 PLACEHOLDER (Hours) Tj

I am fine with creating the script to append a page number at the start of the number But I just can not seem to get the leading and trailing zeros where they need to be. If you could point me in the right direction or give me some suggestions to try I would greatly appreciate any help you can give me.
Thank you very much Paul
# 2  
Old 10-26-2017
Hi, try:
Code:
awk '{$1=sprintf(fmt,$1); $2=sprintf(fmt,$2)}1' fmt='%013.6f' file

# 3  
Old 10-26-2017
Thank you Sir, that is exactly what I needed!!!
I think It's time I learned awk as well.
thanks again
Paul
# 4  
Old 10-26-2017
Similar, but using internals:
Code:
awk '{$1+=1E-7; $2+=1E-7}1' CONVFMT="%013.6f" file

Adding the very small value in lieu of 0 is to provoke floating conversion even when we're having integers.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 10-26-2017
@RudiC, when experimenting I could not get that to work aerlier using +0. Your approach of a small number seems to work.

I found though that if the value is too small then it does not work again, i.e. 1E-12 works, but 1E-13 does not. So One would need to choose carefully, so perhaps it is better not to rely on this..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 10-26-2017
Hi again I understand if you are very busy, could you possibly break down this command for me so I could understand what it is doing please?
# 7  
Old 10-26-2017
Code:
while read a b c
do
   printf "%013.6f %013.6f %s\n" "$a" "$b" "$c"
done < infile

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to trim the decimal place for all the columns?

Dear all, I have a file call test.txt which has 2000 columns, 1000 rows. If I want to trim all the columns to 3 decimal places, how can I do it? I know how to use awk prinf to trim specic columns. But I don't know how to trim all the columns. Thank you. data sample: 0.976004565 9.34567845... (6 Replies)
Discussion started by: forevertl
6 Replies

2. Shell Programming and Scripting

Decimal Padding in Decimal

Hi Experts, I have requirement to pad a decimal number that should have fixed length as 10. if number is 234.234 > 234.234000 if number is 12.4 > 12.4000000 if number is 3456.5678 > 3456.56780 from above example we can see that overall length is 10 and padding is being done right sided of... (2 Replies)
Discussion started by: looney
2 Replies

3. Shell Programming and Scripting

Using awk to place decimal points at proper position

Hi, I have one input file which is delimited by pipe. I want to put decimal points in this input file at particular position in particular column and also get the negative sign (if any) at start of that column. $ cat Input_file.txt 11|10102693|1|20151202|10263204|20151127|N|0001... (7 Replies)
Discussion started by: Prathmesh
7 Replies

4. UNIX for Dummies Questions & Answers

How to set decimal place in awk?

Dear all, I have a data test.txt as below. X22.30799720_T cg03868770 -0.5645412582127 2.4084685750406e-175 X22.30781182_A cg03868770 -0.5620426397492 3.5818034129169e-172 X22.30780724_C cg03868770 -0.5616890165605 2.9765569717858e-168 what I want is: X22.30799720_T cg03868770... (3 Replies)
Discussion started by: forevertl
3 Replies

5. Shell Programming and Scripting

Unable to get the Specific column with 2 decimal place

Hi, I have an issue converting decimal places of a particular column, i am using below script to get the output, but the output is not generating in desired format. awk -F"," 'BEGIN{OFS=","}{if(NR==0)getline;if ($7 != "") {if ($7 > 0) $7=$7/100 ; {printf "%.2f"... (3 Replies)
Discussion started by: rramkrishnas
3 Replies

6. Shell Programming and Scripting

awk if condition match and fix print decimal place

Hi All, I have problem in the middle of implementing to users, whereby the complaint is all about the decimal place which is too long. I need two decimal places only, but the outcome from command is always fixed to 6. See the sample : before: Sort Total Site Sort SortName Parts ... (3 Replies)
Discussion started by: horsepower
3 Replies

7. Shell Programming and Scripting

Place number with awk

Hello, if I've a list of number 23 34 56 78 how I can place a sequence of ordinated number in a boundary column so 1 23 2 34 3 56 4 78 Thanks in advance! (3 Replies)
Discussion started by: cv313x
3 Replies

8. Shell Programming and Scripting

renaming and number padding using directory as guide, help

I need to take a series of image files, all numbered consecuativly, that were recently dumped in a directory and rename them to pieces of the directories path. Assume all directories are structured as this one so that I may use this script to easly sort and rename files. pt.1 path :... (3 Replies)
Discussion started by: TiredOrangeCat
3 Replies

9. Shell Programming and Scripting

add numbers with decimal place in UNIX

i am trying to add numbers with decimal place and I am prompted with an error. this expr command works :add=`expr 1 + 1` :echo $add :2 but when i am trying to add :addThis=`expr 1.1 + 1` :expr: An integer value was expected. is there a way to add numbers with decimal place in UNIX? (4 Replies)
Discussion started by: tads98
4 Replies
Login or Register to Ask a Question