hello everyone,
can anyone help in writing a perl script which automatically replaces the commands in a
file(command file), with the new values. i have written a code but its not working properly.
if anyone could help me plz. i am new to this scripting language.

thanks in advance.

Thanks & Regards
Nisha

Recommended Answers

All 8 Replies

post your perl code and post any error messages you get when you run the perl code.

this is the code i have written, plz check it if it is the correct way to do it.
#!/grid/common/bin/perl
$file="old_data.txt";
$dir="../nishap";
$sub="nisha";
$path="$dir/$sub/$file";

open(OLD_DATA,"$path") or die("cant open the file $path ");
while($line=<OLD_DATA>)
{
chomp($line);
if($line =~/^abc_xyz_cwd$/)
# if($line =~/^(\w+(.*))$/)
{
$data1=$line;
$data1=~s/abc_xyz_cwd/abc_xyzcwd/gi;
print "$data1\n";
exit;
}
if($line=~/^aa_aa$/)
# if($line =~/^(\w+(.*))$/)
{
$data2=$line;
$data2=~s/aa_aa/aaaa/gi;
print"$data2\n";

}
if($line=~/^bb$/)
{
$data3=$line;
$data3=~s/bb/b_b/gi;
print"$data3\n";

}
if($line=~/^c_c$/)
{
$data4=$line;
$data4=~s/c_c/cc/gi;
print"$data4\n";

}
if($line=~/^dd_ee$/)
{
$data5=$line;
$data5=~s/dd_ee/dde_e/gi;
print"$data5\n";

}
}
$new="out_data.txt";
open(OUT_DATA,">>../nishap/nisha/out_data.txt") or die("cant open the file $new");
{
print OUT_DATA "
$data1
$data2
$data3
$data4
$data5
\n";
}
close(OUT_DATA);

Hi,
Please go through this, before posting your code. Its not an assignment, just a prerequisite if you want to get really quick help.

Only with code it is hard to know what is working and what is not. Provide some input(example) details. Post your errors, if any OR if you are getting output, post it and explain, how you want it to be or how different it is from what you want.

katharnakh.

plz check it if it is the correct way to do it.

Sorry, but I am not a mind reader. I have no idea what your code is supposed to do. See katharnakhs post.

Hi,
Please go through this, before posting your code. Its not an assignment, just a prerequisite if you want to get really quick help.

Only with code it is hard to know what is working and what is not. Provide some input(example) details. Post your errors, if any OR if you are getting output, post it and explain, how you want it to be or how different it is from what you want.

katharnakh.

hello,
i have written the code, but the output is not coming properly.

i have to write a script which can automatically update the old file with new values. for example, i have a file which has few commands in it along with other data( like vdd_power; ac_current,etc). now i have to replace these values in that file with the new values(like vddPOWER;AC_current).and everytime i run the script on such file, it should automatically update the file.

when i run this code, i m getting the file with the replaced values but the file is now containing only the new values, all the old data has been overwritten with the new one.and also i have hard coded it.is there any other way to do it? plz help me .

thanks & ragards
Nisha


the code is:-
#!/grid/comon/bin/perl
$dir="../nishap";
$sub="nisha";
$path="$dir/$sub/$file";
#print "$path\n";
open(OLD_DATA1,"$path") or die("cant open the file $path");
#open(OLD_DATA,"../nishap/nisha/old_data.txt") || die "cant open the file $path";
while($line=<OLD_DATA1>)
{
chomp($line);
if($line =~/^abc_xyz_cwd$/)
#if($line =~/^(\w+(.*))$/)
{
# print "$line\n";
$data1=$line;
$data1=~s/abc_xyz_cwd/abc_xyzcwd/gi;
print "$data1\n";

}
if($line=~/^aa_aa$/)
# if($line =~/^(\w+(.*))$/) ----is this correct??
{
$data2=$line;
$data2=~s/aa_aa/aaaa/gi;
print"$data2\n";
}

if($line=~/^bb$/)
{
#print"$line";
$data3=$line;
$data3=~s/bb/b_b/gi;
print"$data3\n";
}

if($line=~/^c_c$/)
{
$data4=$line;
$data4=~s/c_c/cc/gi;
print"$data4\n";
}

if($line=~/^dd_ee$/)
{
$data5=$line;
$data5=~s/dd_ee/dde_e/gi;
print"$data5\n";
}
}

close(OLD_DATA1);

$new="old_data1.txt";
open(OLD_DATA1,">>../nishap/nisha/old_data1.txt") or die("cant open the file $new");
{
print OLD_DATA1 "
$data1
$data2
$data3
$data4
$data5
\n ";
}
close(OLD_DATA1);

I give up, all yours katharnakh.

Hi,
First go through this.

...
when i run this code, i m getting the file with the replaced values but the file is now containing only the new values, all the old data has been overwritten with the new one.and also i have hard coded it.is there any other way to do it? plz help me .

Probably not. Because, from your code it is clear that you are opening your old_data1.txt in 'append'(from your code: open(OLD_DATA1,">>../nishap/nisha/old_data1.txt") or die("cant open the file $new"); ) mode. So, in the old_data1.txt file you should have original values + appended new modified values.

From the above statement it is clear that you are reading one file, doing some modification in the some line(keeping only modified lines in diff. scalars) and closing the file. Again opening the file in appending/output mode and rewriting those scalars which has you modified valuse back. The new values which get appended when you reopen the file in 'append' mode and write those scalar values.
The file should have only* those modified lines/scalar values in the file only when you open the file in output* mode *and* write only those scalar values. Because when you open a file in output mode, the contents of the file will get flushed first and new values are written. You can check this by simply opening file a file which has some data and closing it. You see the contents of the file flushed out without even you attempting to write something. Hope you got this.

What you should do?
In the program you read complete file into an array. Now each line of the file will be in one index of the array(i.e, $array[0] == <first line of the file>). Loop through the array, modify those specific values of array(nothing but, required lines of the file). Close the file and reopen file in OUTPUT mode and write array to the file.
For eg: The following program will comment out the same file living lines having only new-line character(means, im not modifying such lines).

# test.pl
use strict;
use warnings;
use FileHandle;

my $fh = FileHandle->new(".\\test.pl") || die "Cannot open file.";
my @lines = $fh->getlines();
$fh->close();

for my $i(0..$#lines){
	next if($lines[$i] =~ /^\s+$/);
	chomp($lines[$i]);
	$lines[$i] = $lines[$i] . '# CommentLine-' . eval($i+1) . "\n";
}

$fh = FileHandle->new("> .\\test.pl") || die "Cannot open file.";
$fh->print(@lines);
$fh->close();

also i have hard coded it.is there any other way to do it?

It is difficult to give you an idea how you should generalise. But i can tell you, list out distinct commands in your file and analyse the character sequence of it. Come to an approach how do you want to deal with those kind of lines when you encounter instead of hardcoding something.

katharnakh.

hi,
thanku for your help. I need some more help with the perl scripting.
i have to write a script which compares two files. now thw files have the data like
File1:-
.title 'title:VDDL_switched.meas0'
max_rush_current = -5.4316e-02 at = 4.9415e-09
powerup_time_u_switch_ring/u2_hs65_ls_sw3l60 = 6.3951e-09 targ = 6.9051e-09 trig = 5.1000e-10
temper = 2.5000e+01
alter# = 1.0000e+00

File2:-
.title 'title:VDDL_switched.meas0'
max_rush_current = -5.5516e-02 at = 4.9415e-09
pg_cell = 6.3977e-09 targ = 6.9066e-09 trig = 5.1000e-10
temper = 2.5230e+01
alter# = 1.1111e+00

now i have to compare this two files and check whther the value change is within 1% of the initial value.

Plz help me. i have no idea where to start with and what to do!!!

Regards
Nisha.


Hi,
First go through this.


Probably not. Because, from your code it is clear that you are opening your old_data1.txt in 'append'(from your code: open(OLD_DATA1,">>../nishap/nisha/old_data1.txt") or die("cant open the file $new"); ) mode. So, in the old_data1.txt file you should have original values + appended new modified values.

From the above statement it is clear that you are reading one file, doing some modification in the some line(keeping only modified lines in diff. scalars) and closing the file. Again opening the file in appending/output mode and rewriting those scalars which has you modified valuse back. The new values which get appended when you reopen the file in 'append' mode and write those scalar values.
The file should have only* those modified lines/scalar values in the file only when you open the file in output* mode *and* write only those scalar values. Because when you open a file in output mode, the contents of the file will get flushed first and new values are written. You can check this by simply opening file a file which has some data and closing it. You see the contents of the file flushed out without even you attempting to write something. Hope you got this.

What you should do?
In the program you read complete file into an array. Now each line of the file will be in one index of the array(i.e, $array[0] == <first line of the file>). Loop through the array, modify those specific values of array(nothing but, required lines of the file). Close the file and reopen file in OUTPUT mode and write array to the file.
For eg: The following program will comment out the same file living lines having only new-line character(means, im not modifying such lines).

# test.pl
use strict;
use warnings;
use FileHandle;

my $fh = FileHandle->new(".\\test.pl") || die "Cannot open file.";
my @lines = $fh->getlines();
$fh->close();

for my $i(0..$#lines){
	next if($lines[$i] =~ /^\s+$/);
	chomp($lines[$i]);
	$lines[$i] = $lines[$i] . '# CommentLine-' . eval($i+1) . "\n";
}

$fh = FileHandle->new("> .\\test.pl") || die "Cannot open file.";
$fh->print(@lines);
$fh->close();

It is difficult to give you an idea how you should generalise. But i can tell you, list out distinct commands in your file and analyse the character sequence of it. Come to an approach how do you want to deal with those kind of lines when you encounter instead of hardcoding something.

katharnakh.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.