How to pad zeroes based on input string?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to pad zeroes based on input string?
# 1  
Old 02-05-2015
How to pad zeroes based on input string?

Hello everyone,

I am comparing two floating point numbers by storing them in seperate files and then using difference command to verify,it is working fine.

But I want to compare two values which will come at 4 precision places.

Code:
ex:
file1
Date,Count,Checksum
01/31/2014,3702,-170552450514.86
 
command:
cat $trigger_file  | sed '1d' | tr -s ' ' | cut -d, -f3 > trigger_file.txt
-170552450514.86
 
file2
 
sed '1d' ${data_file} | cut -d',' -f7 | awk '{s+=$1}END{ printf("%.4f\n",s)}' > data_file.txt
-170552450514.8600

My requiremnet is

file1

The source file will come at 4 precision values (file1).if the value is 123.42 it will not display as 123.4200 and 123.4 will not be 123.4000 and 0 will not come in as 0.0000

I Need to display it padding with 0's at the end until it becomes 4 precision points .I have searched in this forum for answers before posting, my requirement is based on input,

I need to pad 0's so that i can compare it with file2
ex:
0 should be 0.0000
1.2 should be 1.2000
-5.023 should be -5.0230
6.2345 should be 6.5345

Could you please help me
# 2  
Old 02-05-2015
Code:
awk -F"," ' NR > 1 { printf("%.04f",$3) } ' $trigger_file

# 3  
Old 02-05-2015
What's wrong with applying your (adapted) awk snippet to your trigger file in lieu of that pipe organ? Other than you need to add 0.3 to achieve
Quote:
6.2345 should be 6.5345
# 4  
Old 02-05-2015
Hi Andu23,

Thanks for the reply..

I am not able to display any result with the command.

Code:
 
cat test.txt
01/31/2014,3702,-170552450514.86
 
cib-sokay2{}:awk -F"," ' NR > 1 { printf("%.04f",$3) } ' test.txt
cib-sokay2{}:

Could you please help
# 5  
Old 02-05-2015
If you don't have header in your input file then remove NR > 1 from awk
This User Gave Thanks to anbu23 For This Post:
# 6  
Old 02-05-2015
Thanks Anbu ..its working fine..i am facing a starnge issue nowSmilie

command given by you works fine if there is no control m appended at the end of the line or else it will display 0.0000 ....is there any way i can remove controlm from the file and then use the awk command...i tried with sed but it is not working

Code:
sed 's/^M//g' abc.csv > def.csv
mv def.csv abc.csv
 
awk -F"," ' NR > 1 { printf("%.04f",$3) } ' abc.csv
0.0000
 
vi abc.csv
As Of Date,Count,Checksum^M
01/31/2014,3702,-170552450514.86^M


Last edited by karthik adiga; 02-05-2015 at 06:11 AM.. Reason: output of query
# 7  
Old 02-05-2015
Code:
tr -d '\015' < abc.csv > def.csv
mv def.csv abc.csv

This User Gave Thanks to anbu23 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a string based on input

I am trying to read a value from a mapping file and would need to replace the value based on country parameter source_table_@ctry_final Expected final_var=source_table_aus_final If the country is in nz,usa,uk then final_var=diff_table_nz_final final_var=diff_table_usa_final like that... (10 Replies)
Discussion started by: Master_Mind
10 Replies

2. Shell Programming and Scripting

Insert a user input string after matched string in file

i am having file like this #!/bin/bash read -p 'Username: ' uservar match='<color="red" />' text='this is only a test so please be patient <color="red" />' echo "$text" | sed "s/$match/&$uservar\g" so desireble output what i want is if user type MARIA this is only a test so please... (13 Replies)
Discussion started by: tomislav91
13 Replies

3. Shell Programming and Scripting

How to find previous string based on an input string?

Hi, I did some research but cannot find the right solution so hopefully someone can help me here. I have a long string format like: VAR=111:aaaa,222:bbb,333:ccc it could be VAR=111:aaa,222:bbb,333:ccc,444:ddd, etc what I looking for is eg. if I give ccc, it will return me 333... (2 Replies)
Discussion started by: netbanker
2 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. Shell Programming and Scripting

Pad zeroes first field in a Delimited file

Need help. I tried using an awk command to pad zeroes. Unfortunately, the "|" pipe delimited character is gone when I tried to write the records to another file. awk -F \| ' {$1=sprintf("%06s", $1); print $0}' $CUSTFINAL2 > $CUSTFINAL3 BEFORE "KEYRECORD"|"SA ID"|"PER ID"|"SP ID"|"ACCT... (3 Replies)
Discussion started by: johnhips
3 Replies

6. Shell Programming and Scripting

Pad space at the end of string and reformat

I need to read in the string from input file and reform it by cut each segment and check the last segement lenght. If the last segment length is not as expected (see below segment file or table. It is predefined), then pad enough space. Old string FU22222222CA6666666666AKxvbFMddreeadBP999... (11 Replies)
Discussion started by: menglm
11 Replies

7. Shell Programming and Scripting

Reformat a string and pad space at the end

I need to read in the string from input file and reform it by cut each segment and check the last segement lenght. If the last segment length is not as expected (see below segment file or table. It is predefined), then pad enough space. Old string FU22222222CA6666666666AKxvbFMddreeadBP999... (1 Reply)
Discussion started by: menglm
1 Replies

8. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

9. Shell Programming and Scripting

input a string and copy lines from a file with that string on it

i have a file1 with many lines. i have a script that will let me input a string. for example, APPLE. what i need to do is to copy all lines from file1 where i can find APPLE or any string that i specify and paste in on file 2 thanks in advance! (4 Replies)
Discussion started by: engr.jay
4 Replies

10. Shell Programming and Scripting

insert leading zeroes based on the character count

Hi, I need add leading zeroes to a field in a file based on the character count. The field can be of 1 character to 6 character length. I need to make the field 14bytes. eg: 8351,20,1 8351,234,6 8351,2,0 8351,1234,2 8351,123456,1 8351,12345,2 This should become. ... (3 Replies)
Discussion started by: gpaulose
3 Replies
Login or Register to Ask a Question