Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^3: sed command with variable in perl

by samira_saber (Novice)
on Aug 16, 2017 at 05:14 UTC ( #1197487=note: print w/replies, xml ) Need Help??


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

Hi AnomalousMonk

....... using warning; first of all , i used that line before ( use strict; use warning;) at the top of my Perl code and for this reason I omit them. I think i should explain a little bit about my code. I have to m file (from matlab) and I just want to make them work automate as a software. I am very very new into Perl. For this reason I used linux shell commands and it worked fine. Now , There is problem with my code.

.... File name argument I will put my file name in the correct place , thank you.

..... substitution operation without something to substitute As i mentioned before, I have two files each of them has spciefic SOL number which I used grep command to count them and then use it as a variable. $cmd =("grep -e HW1 myhalf1.pdb | wc -l > text1.txt") ; open FILE, "<text1.txt"; my $sol1= do {local $/; <FILE>}; print p ("sol1=$sol1"); As you see I have it before. in the code, which you had posted i just omit this line my $sol1 = 'my new stuff'; because i introduce my variable before. and the next line which have $sol1 is remained intact. s{ (SOL) ^\n* }{$1 $sol1}xmsg;

......about errors i ve added the code into my .pl file and run it. After that I check error_log file in the server and there is no warning. I have just changed the line into this my $filename_in = 'topol1.top'; no error.even not making another file. and i ve checked the file permission too.no problem about the permission.

  • Comment on Re^3: sed command with variable in perl

Replies are listed 'Best First'.
Re^4: sed command with variable in perl
by poj (Abbot) on Aug 16, 2017 at 09:51 UTC

    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
      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:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1197487]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2022-01-27 21:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (71 votes). Check out past polls.

    Notices?