LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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, 09:37 AM   #1
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Rep: Reputation: 32
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, 01:11 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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, 05: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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
BASH: Read entire file line in for loop clinton Programming 16 04-18-2013 01:06 PM
how to add numbers in a formatted file via a bash script? zero79 Linux - General 8 12-24-2010 06:48 PM
Decimal numbers in bash script variables? Massif Programming 3 11-07-2005 10:01 PM
How to use decimal numbers in in shell scripts a1ex_007 Programming 6 03-11-2005 10:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:43 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration