Sponsored Content
Top Forums Shell Programming and Scripting Check for decimal point and add it at the end if its not there using awk/perl Post 302947170 by nvk_vinoth on Tuesday 16th of June 2015 06:25:03 AM
Old 06-16-2015
Check for decimal point and add it at the end if its not there using awk/perl

I have test.dat file with values given below:
Code:
20150202,abc,,,,3625.300000,,,,,-5,,,,,,,,,,,,,,,,,,,,,,
20150202,def,,,,32.585,,,,,0,,,,,,,,,,,,,,,,,,,,,,
20150202,xyz,,,,12,,,,,0.004167,,,,,,,,,,,,,,,,,,,,,,

My expected output is shown below:

Code:
20150202,abc,,,,3625.300000,,,,,-5.,,,,,,,,,,,,,,,,,,,,,,
                                  ^. added here
20150202,def,,,,32.585,,,,,0.,,,,,,,,,,,,,,,,,,,,,,
                            ^. added here
20150202,xyz,,,,12.,,,,,0.004167,,,,,,,,,,,,,,,,,,,,,,
                  ^. added here

So if column 6 and 11 doesn't have decimal point in it, then we should add '.' at the end of the file.

I have tried below code but it's throwing error message during split

Code:
#!/usr/bin/perl
use strict;
use warnings;
my $filename = 'test.dat';
open my $fh, $filename or die "Could not open file '$filename': $!";
my @cols_to_change = qw ( 5 10 );
while (my $val = <$fh>) {
   my @row = split (/,/);
   foreach my $col ( @cols_to_change ) {
      unless ( $row[$col] =~ m/\./ ) { $row[$col] .= '.' }
   }
   print join ( ',', @row );
}

Error message is given below:
Code:
Use of uninitialized value in split at test.pl line 11, <$fh> line 1.
Use of uninitialized value in pattern match (m//) at test.pl line 13, <$fh> line 1.
Use of uninitialized value in pattern match (m//) at test.pl line 13, <$fh> line 1.
Use of uninitialized value in join or string at test.pl line 15, <$fh> line 1.
Use of uninitialized value in join or string at test.pl line 15, <$fh> line 1.
Use of uninitialized value in join or string at test.pl line 15, <$fh> line 1.
Use of uninitialized value in join or string at test.pl line 15, <$fh> line 1.
Use of uninitialized value in join or string at test.pl line 15, <$fh> line 1.
Use of uninitialized value in join or string at test.pl line 15, <$fh> line 1.
Use of uninitialized value in join or string at test.pl line 15, <$fh> line 1.
Use of uninitialized value in join or string at test.pl line 15, <$fh> line 1.
Use of uninitialized value in join or string at test.pl line 15, <$fh> line 1.

Could you please analyze the issue and provide solution asap? I am happy if this could be done using awk but I am NOT allowed to use any additional perl modules in my environment E.g: Text::CSV.

Regards,
Vino.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

how to get rid of decimal point?

Hi, I have input with decimal point ( 9.99 ) for hours variable hrs. I need to change it to seconds. Here is my code: secs=`/usr/ucb/echo $hrs*3600 |bc` But I don't want to see the decimal point. I can use awk to trim it if there is one. I am just wondering if there is better standard... (2 Replies)
Discussion started by: cin2000
2 Replies

2. Shell Programming and Scripting

Insert a decimal point

Hi all. Using /bin/sh on an HPUX system. I want to place a decimal in the field 2 charactors from the right (yes, converting to currency). The field lengths are variable. Here's what I'm doing: exec < filename while read FIELD1 FIELD2 do FIELD1="echo $FIELD1 | sed 'syntax that will... (4 Replies)
Discussion started by: lyoncc
4 Replies

3. Shell Programming and Scripting

Comparing two numbers with decimal point

How to compare two numbers with decimal points ? Is there a way in bash to do this? (33 Replies)
Discussion started by: kinny
33 Replies

4. Shell Programming and Scripting

Insert decimal point for numbers

Hi In Unix, I have a file with some numbers like : 45600 12345 I want to insert a decimal point for these numbers based on user input. If the input is 2, the numbers should be changed to 456.00 123.45 If the input is 3, the numbers should be changed to 45.600 12.345 Can... (2 Replies)
Discussion started by: yoursdivu
2 Replies

5. Shell Programming and Scripting

using awk to strip out decimal point and trailing integers

Hey guys, I've got an awk string that does a simple calculation which works well. However, I'd really like to be able to strip the decimal point and the trailing ints from it - any ideas on how I'd change the awk string to be able to do that ? I've changed it countless times and each time it... (14 Replies)
Discussion started by: jimbob01
14 Replies

6. Shell Programming and Scripting

How to perform a hexdump using dd from start point to end point?

hi, I would like to ask or is it possible to dump a hex using dd from starting point to end point just like the "xxd -s 512 -l 512 <bin file>" I know the redirect hexdump -C but i can't figure it out the combination options of dd. Hope someone can share their knowledge.. Thanks in... (3 Replies)
Discussion started by: jao_madn
3 Replies

7. Shell Programming and Scripting

awk decimal point numbers matching

Hi All, Can some one help me in identifying the significance of character "$" ,Which is playing critical role in matching decimal point numbers as below. $ echo "01#.01"|awk '{if ($0 ~ /^+(\.*)?$/) print}' $ echo "01#.01"|awk '{if ($0 ~ /^+(\.*)?/) print}' 01#.01 $ Regards, Rmkganesh. (3 Replies)
Discussion started by: rmkganesh
3 Replies

8. Shell Programming and Scripting

Add Delimiter after 2 decimal point for a particular column

Hi All, Please help for the following scenario where I want to add a delimiter after 2 decimal point integer. Original Line 1007937820§L§2016-12-19§000000002§2018-02-01§2050-12-01§00395§M§146713.57§00005.05000§762.59§00395§M§301223.0528§1165§2017-03-31 20:34:25 Expected Line... (12 Replies)
Discussion started by: ckwan123
12 Replies

9. UNIX for Beginners Questions & Answers

How to add following decimal point to a CSV value?

hi there I being trying to figure out way to add " .0" to an integer value in a csv using sed or awk with out success. just as a work around for 2147483647 32 bit limitation that influxdb is currently having the data base will accept values and work fine if it has the XXX.0 ... (7 Replies)
Discussion started by: sash99
7 Replies

10. UNIX for Advanced & Expert Users

Convert a numeric to 2 decimal point value

Hi , I have a file which contains text like A|Mau|Code|12|Detail B|Mau|Code|20|Header I want to write a command using awk which will output A|Mau|Code|12.00|Detail B|Mau|Code|20.00|Header I used a command like awk -F"|" {printf "%s|%s|%s|%.2f|%s",$1,$2,$3,$4,$5}' which does the... (4 Replies)
Discussion started by: LoneRanger
4 Replies
IGAWK(1)							 Utility Commands							  IGAWK(1)

NAME
igawk - gawk with include files SYNOPSIS
igawk [ all gawk options ] -f program-file [ -- ] file ... igawk [ all gawk options ] [ -- ] program-text file ... DESCRIPTION
Igawk is a simple shell script that adds the ability to have ``include files'' to gawk(1). AWK programs for igawk are the same as for gawk, except that, in addition, you may have lines like @include getopt.awk in your program to include the file getopt.awk from either the current directory or one of the other directories in the search path. OPTIONS
See gawk(1) for a full description of the AWK language and the options that gawk supports. EXAMPLES
cat << EOF > test.awk @include getopt.awk BEGIN { while (getopt(ARGC, ARGV, "am:q") != -1) ... } EOF igawk -f test.awk SEE ALSO
gawk(1) Effective AWK Programming, Edition 1.0, published by the Free Software Foundation, 1995. AUTHOR
Arnold Robbins (arnold@skeeve.com). Free Software Foundation Nov 3 1999 IGAWK(1)
All times are GMT -4. The time now is 10:14 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy