Solved

perl or shell to round last column to hundredths place

Posted on 2011-09-22
4
316 Views
Last Modified: 2012-05-12
perl or shell to round last column to hundredths place. I have a sample file below.
so4pap.txt
0
Comment
Question by:libertyforall2
4 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 36584933
What have you tried?
0
 
LVL 14

Accepted Solution

by:
Dushyant Sharma earned 250 total points
ID: 36585708
awk '{print $1 " " $2 " " $3 " "  $4/1}' so4pap.txt

you need to set the scale
0
 
LVL 9

Assisted Solution

by:parparov
parparov earned 250 total points
ID: 36588104
Usage: script.pl < so4pap.txt

#!/usr/bin/perl

use strict;
use warnings;

while (<>) {
	chomp;
	my @data = split(/\s+/);
	$data[-1] = sprintf("%0.2f\n", $data[-1]);
	print join(" ", @data);
}

Open in new window

0
 

Author Closing Comment

by:libertyforall2
ID: 36898437
Great!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question