The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to number format a data file without using SED? Cactus Jack UNIX for Dummies Questions & Answers 3 01-12-2008 07:47 PM
Number format conversion in UNIX kamal_418 Shell Programming and Scripting 1 01-09-2008 04:12 AM
printing format for hexadecimal signed number user_prady Shell Programming and Scripting 10 11-08-2007 02:34 PM
Help with format a number in a file kabatsie Shell Programming and Scripting 3 09-07-2007 05:19 AM
How to format number/string in ksh GNMIKE Shell Programming and Scripting 2 07-03-2005 04:44 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-13-2008
jinsh jinsh is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 6
number format in Perl

I try to read in a file and write out a new file with increased number at the end of each line. And I can set the initial value and increased constant from inputs.

input file:
text1
text2
text3
...
text100

if I set initial value is 10, and increased constant is 0.4
output file:
text1 10
text2 10.4
text3 10.8
...
text100 49.6

Code:
if ($#ARGV != 3) {
print "usage: do inputfile initial step outputfile\n";
exit;
}

$infile = $ARGV[0];
$initial = $ARGV[1];
$cons = $ARGV[2];
$outfile = $ARGV[3];

open(OF, $infile);
open(NF, ">$outfile");

my $offset = $initial;

# read in each line of the file
while ($line = <OF>) {
$line =~ s/$/ $offset/;
$offset += $cons;
print NF $line;
}

close(OF);
close(NF);

But my output like:
text1 10
text2 10.4
text3 10.8
...
text54 31.1999999999999
text55 31.5999999999999
text56 31.9999999999999
text57 32.3999999999999
text58 32.7999999999999
...
text100 49.5999999999999

How to keep 1 decimal digit output? And why get this result?

Thanks,
  #2 (permalink)  
Old 02-13-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
use sprintf or printf:

Code:
my $n = 10;
for (1 .. 100) {
   $n += .4;
   printf "%.1f\n", $n
}
  #3 (permalink)  
Old 02-13-2008
jinsh jinsh is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 6
but I need show text1 ~ text100 in output file, the increased number be added at the end of each text.
  #4 (permalink)  
Old 02-13-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
my example is a helping hand, not a complete solution. But what you want to do should be very easy, give it try.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:15 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0