Sponsored Content
Full Discussion: Decimal field round of
Top Forums Shell Programming and Scripting Decimal field round of Post 302996814 by drl on Tuesday 2nd of May 2017 11:10:29 PM
Old 05-03-2017
Hi.

Like Aia I did not seem have trouble. I used a module that recognizes numerics:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate formatting reals.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C perl dixf

FILE=${1-data1}

pl " Perl code p1:"
cat ./p1

pl " Input data file $FILE:"
cat $FILE

pl " Results:"
./p1 data1

pl " Details for ./p1:"
dixf ./p1

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.7 (jessie) 
bash GNU bash 4.3.30
perl 5.20.2
dixf (local) 1.42

-----
 Perl code p1:
#!/usr/bin/env perl

# @(#) p1       Demonstrate recognition of reals, round to lower precision.
# Problem with 10463.580902756539
# $decimal_line = sprintf( "%.2f", $decimal_line ) if ( $decimal_line =~ /[-+]?[0-9]*(\.[0-9][0-9]+$)/ ) ;

use strict;
use warnings;
use Regexp::Common qw /number/;

my ( @a, $i );

while (<>) {
  chomp;
  @a = split(/,/);
  for ( $i = 0; $i <= $#a; $i++ ) {
    if ( $a[$i] =~ /^$RE{num}{real}$/ ) {       # format if real
      printf( "%.2f", $a[$i] );
      print "," if $i < $#a;
    }
    else {
      printf( "%s", $a[$i] );
      print "," if $i < $#a;
    }
  }
  print "\n";
}

-----
 Input data file data1:
26200000,1,14771,12/31/2016,N,3.542377036225,0.840074561430,-1.236050220213,4.214781311068,,,,,,,,,,,4.635809027565,,,,,,,,,4.635809027565,XXXX,02/09/2017,,,,,,,,,A,XX,10463.580902756539

-----
 Results:
26200000.00,1.00,14771.00,12/31/2016,N,3.54,0.84,-1.24,4.21,,,,,,,,,,,4.64,,,,,,,,,4.64,XXXX,02/09/2017,,,,,,,,,A,XX,10463.58

-----
 Details for ./p1:
p1      Demonstrate recognition of reals, round to lower precision. (what)
Path    : ./p1
Version : 13.
Length  : 27 lines
Type    : Perl script, ASCII text executable
Shebang : #!/usr/bin/env perl
Modules : (for perl codes)
 strict 1.08
 warnings       1.23
 Regexp::Common 2013031301

But comments like ... rounding off all the decimal fields except the last one which is 10463.580902756539 ... that do not explicitly state what the result/problem was does not give us much to go on.

If you had trouble, I wonder if it is a 32-bit problem, but that's just a guess.

Best wishes ... cheers, drl
 

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

incremental addition of hex decimal number in one field

Hi I want to incremental add hex decimal number to a particula field in file eg: addr =123 dept1=0 addr = 345 dept2 =1 addr2 = 124 dept3 =2 . . . . . . addr3 =567 dept15 =f Is there any command which add... (8 Replies)
Discussion started by: diddi_linux
8 Replies

2. Shell Programming and Scripting

Round off the a Decimal value.

HI, I have a script which is used to calculate the Memory & CPU utilization a server. memx=`ssh -l siebel1 ${f} /usr/sbin/prtconf|grep -i 'Memory size'|tr -s " "|/usr/xpg4/bin/awk -F" " '{print $3 * 1024}'` v5=`ssh -l siebel1 ${f} vmstat 1 2 | tail -1 | tr -s " " | /usr/xpg4/bin/awk -v... (3 Replies)
Discussion started by: dear_abhi2007
3 Replies

3. Shell Programming and Scripting

round decimal values and use in loops

i have a file in which 3 values are stored like --4.72 4.42 3.86 what i wanna do is that take read each value and compare with a fixed value say 5 but cant do so as i am getting an error for the same. please check the code cat /home/nsadm/auto/Logging/uptime.txt| while read a b c do if... (2 Replies)
Discussion started by: gemnian.g
2 Replies

4. Shell Programming and Scripting

need to convert a decimal value to an ascii char with awk for the field delimiter

Hello, I need an awk script to receive a variable that's an decimal value such as 009 or 031 and then convert this value to an ascii character to use as the FS (field separator for the input file). For example, 009 should be converted to an ascii tab 031 should be converted to an ascii... (1 Reply)
Discussion started by: script_op2a
1 Replies

5. Shell Programming and Scripting

To remove decimal values from the field

Hi Friends, Hi Friends, I have a file in the following format file.txt 1|jHDJ|1345.0000000|384837843|39084938 2|jkaehjk|5784.00000|jhejhwej|3398934 i want to remove the 3rd field data decimal points from all lines output.txt 1|jHDJ|1345|384837843|39084938... (5 Replies)
Discussion started by: i150371485
5 Replies

6. Shell Programming and Scripting

How to round up value upto 2 decimal places using sed?

Please help me in rounding up value upto 2 decimal palces using sed command #!/usr/bin/bash a=15.42 b=13.33 c=`echo $a*$b |bc -l` echo $c above code is is giving output "205.5486" but i want the output as "205.55" Thank you... (15 Replies)
Discussion started by: ranabhavish
15 Replies

7. Shell Programming and Scripting

Help with Round Up with 2 decimal point at specific column

Input file: USA 20.5683 UK 3.54221 Japan 2.54001 China 2.50897 Germany 2.05816 . . Desired output file: USA 20.57 UK 3.54 Japan 2.54 China 2.51 Germany 2.06 . . (2 Replies)
Discussion started by: perl_beginner
2 Replies

8. UNIX for Beginners Questions & Answers

Decimal numbers and letters in the same collums: round numbers

Hi! I found and then adapt the code for my pipeline... awk -F"," -vOFS="," '{printf "%0.2f %0.f\n",$2,$4}' xxx > yyy I add -F"," -vOFS="," (for input and output as csv file) and I change the columns and the number of decimal... It works but I have also some problems... here my columns ... (7 Replies)
Discussion started by: echo manolis
7 Replies
basename(1)							   User Commands						       basename(1)

NAME
basename, dirname - deliver portions of path names SYNOPSIS
/usr/bin/basename string [suffix] /usr/xpg4/bin/basename string [suffix] dirname string DESCRIPTION
The basename utility deletes any prefix ending in / and the suffix (if present in string) from string, and prints the result on the stan- dard output. It is normally used inside substitution marks (``) within shell procedures. /usr/bin The suffix is a pattern defined on the expr(1) manual page. /usr/xpg4/bin The suffix is a string with no special significance attached to any of the characters it contains. The dirname utility delivers all but the last level of the path name in string. EXAMPLES
Example 1 Setting environment variables The following example, invoked with the argument /home/sms/personal/mail sets the environment variable NAME to the file named mail and the environment variable MYMAILPATH to the string /home/sms/personal: example% NAME=`basename $HOME/personal/mail` example% MYMAILPATH=`dirname $HOME/personal/mail` Example 2 Compiling a file and moving the output This shell procedure, invoked with the argument /usr/src/bin/cat.c, compiles the named file and moves the output to cat in the current directory: example% cc $1 example% mv a.out `basename $1 .c` ENVIRONMENT VARIABLES
See environ(5) for descriptions of the following environment variables that affect the execution of basename and dirname: LANG, LC_ALL, LC_CTYPE, LC_MESSAGES, and NLSPATH. EXIT STATUS
The following exit values are returned: 0 Successful completion. >0 An error occurred. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: /usr/bin +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ /usr/xpg4/bin +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWxcu4 | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ SEE ALSO
expr(1), basename(3C), attributes(5), environ(5), standards(5) SunOS 5.11 18 Mar 1997 basename(1)
All times are GMT -4. The time now is 03:15 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy