Today's Posts

Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

Convert a numeric to 2 decimal point value

advanced, awk, convert, decimal, decimal point, numeric, shell, unix

Login to Reply

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

Hi , I have a file which contains text like


I want to write a command using awk which will output


I used a command like
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; 1 Week Ago at 03:49 PM.. Reason: code tags
# 2  
Old 1 Week Ago
awk -F"|" -v OFS="|" '{ $4=sprintf("%.2f",$4); } 1'

The Following User Says Thank You to Corona688 For This Useful Post:
LoneRanger (1 Week Ago)
# 3  
Old 1 Week Ago
Your approach needs two small corrections to print the desired output:
awk -F"|" '{printf "%s|%s|%s|%.2f|%s\n",$1,$2,$3,$4,$5}' file

A wee bit shorter would be
$ awk -F\| '$4+=1e-4' CONVFMT="%.2f" OFS="|" file

(will fail on $4 == 0.0001, though)
# 4  
Old 1 Week Ago
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  
Old 1 Week Ago
awk works on pattern {action} pairs in a way that it evaluates the pattern, and, if TRUE, executes the action.

man awk:
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 to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to add following decimal point to a CSV value? sash99 UNIX for Beginners Questions & Answers 7 08-18-2018 04:18 AM
Add Delimiter after 2 decimal point for a particular column ckwan123 Shell Programming and Scripting 12 04-05-2017 12:18 AM
Moving decimal point in a series of numbers Board27 Shell Programming and Scripting 1 11-26-2016 08:13 AM
Help with Round Up with 2 decimal point at specific column perl_beginner Shell Programming and Scripting 2 04-27-2016 04:53 AM
Urgent help needed.. C++ program to convert decimal to hexa decimal greenworld123 Programming 1 12-12-2014 09:34 PM
Convert hexa decimal to decimal sudhakar T UNIX for Dummies Questions & Answers 4 03-26-2014 01:06 PM
awk decimal point numbers matching rmkganesh Shell Programming and Scripting 3 09-17-2013 02:38 AM
Retain upto 2 decimal point for numeric values ningy Shell Programming and Scripting 3 10-11-2012 04:04 AM
Convert decimal notation to ANSI point code notation aavam Shell Programming and Scripting 4 06-28-2011 10:16 PM
Insert decimal point for numbers yoursdivu Shell Programming and Scripting 2 02-05-2010 07:08 AM
Comparing two numbers with decimal point kinny Shell Programming and Scripting 33 11-21-2009 10:42 AM
PERL:How to convert numeric values txt file to PACKED DECIMAL File? aloktiwary Shell Programming and Scripting 1 05-20-2009 10:55 AM
Insert a decimal point lyoncc Shell Programming and Scripting 4 06-04-2007 06:40 PM
how to get rid of decimal point? cin2000 Shell Programming and Scripting 2 12-27-2005 12:26 PM
Floating point numeric comparisions in bash borncrazy Shell Programming and Scripting 2 03-27-2005 07:39 PM

All times are GMT -4. The time now is 08:53 AM.

Unix & Linux Forums Content Copyright 1993-2018. All Rights Reserved.
Show Password