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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
04-30-2007, 09:37 AM
|
#1
|
Member
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232
Rep:
|
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.
|
|
|
04-30-2007, 01:11 PM
|
#2
|
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
|
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.
|
|
|
04-30-2007, 05:04 PM
|
#3
|
Member
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221
Rep:
|
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
}
|
|
|
All times are GMT -5. The time now is 11:43 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|