Convert a numeric to 2 decimal point value


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Convert a numeric to 2 decimal point value

Hi , I have a file which contains text like

Code:
A|Mau|Code|12|Detail
B|Mau|Code|20|Header

I want to write a command using awk which will output

Code:
A|Mau|Code|12.00|Detail
B|Mau|Code|20.00|Header

I used a command like
Code:
awk -F"|" {printf "%s|%s|%s|%.2f|%s",$1,$2,$3,$4,$5}'

which does the job but could have been better.

any suggestions?

Last edited by Scrutinizer; 11-08-2018 at 02:49 PM.. Reason: code tags
# 3  
Your approach needs two small corrections to print the desired output:
Code:
awk -F"|" '{printf "%s|%s|%s|%.2f|%s\n",$1,$2,$3,$4,$5}' file

A wee bit shorter would be
Code:
$ awk -F\| '$4+=1e-4' CONVFMT="%.2f" OFS="|" file
A|Mau|Code|12.00|Detail
B|Mau|Code|20.00|Header

(will fail on $4 == 0.0001, though)
# 4  
Can you please explain what does "1" at the end does? I can see that i dont need to write print $0 to print the complete line
# 5  
awk works on pattern {action} pairs in a way that it evaluates the pattern, and, if TRUE, executes the action.

man awk:
Quote:
One, but not both, of pattern {action} can be omitted. If {action} is omitted it is implicitly { print }. If pattern is omitted, then it is implicitly matched.

So - as 1 represents TRUE, awk executes the default action print.
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Urgent help needed.. C++ program to convert decimal to hexa decimal
greenworld123
Hi , seq can be 0...128 int windex = seq / 8; int bindex = seq % 8; unsigned char bitvalue = '\x01' << (7-bindex) ; bpv.bitmapvalue = bitvalue; This is the part of a program to convert decimal to bitmap value of hexadecimal. I want this to change to convert only to...... Programming
1
Programming
Convert hexa decimal to decimal
sudhakar T
Hi, I want to convert two hexadecimal numbers to decimal using unix command line. 1cce446295197a9d6352f9f223a9b698 fc8f99ac06e88c4faf669cf366f60d I tried using `echo "ibase=16; $no |bc` printf '%x\n' "1cce446295197a9d6352f9f223a9b698" but it doesn't work for such big number it...... UNIX for Dummies Questions & Answers
4
UNIX for Dummies Questions & Answers
Retain upto 2 decimal point for numeric values
ningy
Hi I have a big file with lines like below <tr align="center" bgcolor="SEASHELL"><td>94% </td><td>4.62178 </td><td>73.4375 </td></tr> <tr align="center" bgcolor="SEASHELL"></td><td>97% </td><td>3.2962 </td><td>125 </td></tr> I want to format the file such that i get like below. i.e...... Shell Programming and Scripting
3
Shell Programming and Scripting
Convert decimal notation to ANSI point code notation
aavam
wondering if anyone has any thoughts to convert the below thru a shell script Convert decimal signalling point notation to ANSI point code notation There is a site that does that conversion but i need to implement the solution in a shell script.....Thoughts.... OS: Solaris 9 ...... Shell Programming and Scripting
4
Shell Programming and Scripting
PERL:How to convert numeric values txt file to PACKED DECIMAL File?
aloktiwary
Is there any way to convert numeric values txt file to PACKED DECIMAL File using PERL. Regards, Alok... Shell Programming and Scripting
1
Shell Programming and Scripting