Check for decimal point and add it at the end if its not there using awk/perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check for decimal point and add it at the end if its not there using awk/perl
# 1  
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.
# 2  
Old 06-16-2015
Sounds like homework, is it?
If so, please see the reagarding subforum after reading ist sticky.

Thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question