LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-30-2007, 08:37 AM   #1
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Rep: Reputation: 30
bash scripting: loop over a file, replacing two decimal numbers


Hi everyone,

I've been struggling to get the following done in a bash script:

I have a file with some data, and I want to replace two lines in that file so that it can be input to another program. But this replacement needs to be done 20 times, so 20 different replacements.

The lines are:

radius 1 0.57 1.40
...
...
radius 39 1.10 2.10

The two decimal numbers (in the last two columns) should be multiplied by the number x, which takes values
1, 1.05, 1.10,..., 1.50


The looping part is easy, but the actual replacement is what I'm having problem with. I know I should use sed or awk and bc. But how?

Thanks for your help.
 
Old 04-30-2007, 12:11 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Bash & sed are probably not up to the task, since neither of them is capable of floating point arithmetic. Awk or perl would probably be suitable. Your description of the requirement is still a little vague but this snippet of perl might get you somewhere close.

Code:
#! /usr/bin/perl -w

use strict;

 open( INFILE, $ARGV[0] ) or die "Cannot open $ARGV[0] : $!\n";
 my @inFile = <INFILE>;
 close INFILE;
 for( my $i = 1; $i <= 20; $i++ ){
  my @outFile = ();
  my $factor = 1 + ( .05 * $i );
  foreach my $inLine ( @inFile ){
    if( $inLine =~ m/radius\s+1\s+(0\.57)\s+(1\.40)/ ){
      my $col3 = $1 * $factor;
      my $col4 = $2 * $factor;
      push @outFile, "radius 1 $col3 $col4\n";
    }
    elsif( $inLine =~ m/radius\s+39\s+(1\.10)\s+(2\.10)/ ){
      my $col3 = $1 * $factor;
      my $col4 = $2 * $factor;
      push @outFile, "radius 39 $col3 $col4\n";
    }
    else{
      push @outFile, $inLine;
    }
  }
  open( OUTFILE, ">$ARGV[0]_$i" ) or die "Cannot write file '$ARGV[0]_$i' : $!\n";
  print OUTFILE @outFile;
  close OUTFILE;
 }
It should read the file, and create 20 new files subscripted with the index number, and with the numbers adjusted per the index.
--- rod.
 
Old 04-30-2007, 04:04 PM   #3
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by frankie_DJ
Hi everyone,

I've been struggling to get the following done in a bash script:

I have a file with some data, and I want to replace two lines in that file so that it can be input to another program. But this replacement needs to be done 20 times, so 20 different replacements.

The lines are:

radius 1 0.57 1.40
...
...
radius 39 1.10 2.10

The two decimal numbers (in the last two columns) should be multiplied by the number x, which takes values
1, 1.05, 1.10,..., 1.50


The looping part is easy, but the actual replacement is what I'm having problem with. I know I should use sed or awk and bc. But how?

Use awk:

Code:
{
  $2 *= nextval
  $3 *= nextval
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH: Read entire file line in for loop clinton Programming 16 04-18-2013 12:06 PM
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 8 04-05-2011 09:18 AM
how to add numbers in a formatted file via a bash script? zero79 Linux - General 8 12-24-2010 05:48 PM
Decimal numbers in bash script variables? Massif Programming 3 11-07-2005 09:01 PM
How to use decimal numbers in in shell scripts a1ex_007 Programming 6 03-11-2005 09:05 AM


All times are GMT -5. The time now is 07:35 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration