Forum Moderators: bakedjake

Message Too Old, No Replies

Script to edit files

Change a text file using a script

         

octavius2k6

5:02 pm on Oct 27, 2006 (gmt 0)

10+ Year Member



Hello. I need some help. How I can edit a file using a shell script?.
I want to change the "00" in the lines of a makefile and replace it by "01", "02", "03",... and write the changes in news files (ex. make01, make02,..) Thanks a lot for any help!

-------------------------------
NCLIB=-L/home/octavius/netcdf/src/libsrc -lnetcdf
NCINC=-I/home/octavius/netcdf/src/f90
-I/home/octavius/df/src/libsrc

FFLAGSM = -fast
DEFINES= $(DEFS1) $(DEFS)
FFLAGS = $(FPPON) $(DEFINES) $(FFLAGSM)

FC=pgf90

LODCAOBJS = Read00.o

read00: $(LODCAOBJS)
$(FC) $(FFLAGS) $(NCINC) -o $@ $(LODCAOBJS) $(NCINB) $(NCLIB)

Read00.o:Read00.f
pgf90 $(NCINC) $(FFLAGS) -c Read00.f
-------------------------------------

physics

11:12 pm on Oct 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can do this with perl.

Save the following as replace.pl then
chmod 755 replace.pl
perl replace.pl make


#!/usr/bin/perl
# warning, code comes with no warranty!

my $nmake = 20; # number of output make files, only works for less than 100 now

my $template="";
while(<>){
$template .= $_;
}

for(my $i = 0; $i < $nmake; $i++){
my $newfile = $template;
my $istring=$i;
if($i < 10){
$istring = "0$i";
}
$newfile =~ s/00/$istring/mgs;
my $filename = "make$istring";
open OUT, ">$filename" or die "can't open $filename $!";
print OUT $newfile;
close OUT;
}