http://www.perlmonks.org?node_id=1197495


in reply to Re^3: sed command with variable in perl
in thread sed command with variable in perl

Try

#!/usr/bin/perl use strict; use warnings; my $filename_pdb = 'myhalf1.pdb'; my $filename_in = "topol1.top"; my $filename_out = "$filename_in.new"; open my $fh_pdb, '<', $filename_pdb or die "opening '$filename_pdb': $!"; my $sol1 = grep /HW1/,<$fh_pdb>; close $fh_pdb; print "\$sol1 = $sol1\n"; open my $fh_in, '<', $filename_in or die "opening '$filename_in': $!"; open my $fh_out, '>', $filename_out or die "opening '$filename_out': $!"; print "processing '$filename_in' \n"; my $count = 0; while (<$fh_in>) { if ( s/SOL .*/SOL $sol1/g ){ # change as required ++$count; } print $fh_out $_; } close $fh_in; close $fh_out; print "$count lines changed\n";
poj

Replies are listed 'Best First'.
Re^5: sed command with variable in perl
by samira_saber (Novice) on Aug 16, 2017 at 10:31 UTC
    Dear AnomalousMonk, Yes it worked, and I am so happy. Thank you for your big help. I cant believe that it is solved finally. It is more than one week which I try different solutions and you did it.

      Actually, the monk you have to thank is poj, for it is the code in his or her reply that seems to have done the trick. But if I have been of any help at all, I'm quite satisfied.


      Give a man a fish:  <%-{-{-{-<