Long (very long) version. Use regexen. File process_file_1.pl:

use warnings; use strict; die "usage: perl $0 file.name \n" unless @ARGV; my $filename_in = shift; print "processing '$filename_in' \n"; my $filename_out = "$filename_in.new"; open my $fh_in, '<', $filename_in or die "opening '$filename_in': $! +"; open my $fh_out, '>', $filename_out or die "opening '$filename_out': $ +!"; my $sol1 = 'my new stuff'; while (<$fh_in>) { s{ (SOL) [^\n]* }{$1 $sol1}xmsg; print $fh_out $_ or die "writing '$filename_out': $!"; } close $fh_in or die "closing '$filename_in': $!"; close $fh_out or die "closing '$filename_out': $!"; rename $filename_in, "$filename_in.bak" or die "renaming '$filename_in +': $!"; rename $filename_out, $filename_in or die "renaming '$filename_ou +t': $!";
This has all the bells and whistles. Shorter versions (including a one-liner) are possible depending on your exact needs. BTW: What version of Perl are you using?

Usage session:

c:\@Work\Perl\monks\samira_saber>dir Volume in drive C is Acer Volume Serial Number is 9480-355B Directory of c:\@Work\Perl\monks\samira_saber 08/14/2017 05:23 PM <DIR> . 08/14/2017 05:23 PM <DIR> .. 08/14/2017 05:04 PM 759 process_file_1.pl 08/14/2017 04:40 PM 46 topol1.au 08/14/2017 04:40 PM 46 topol1.top 3 File(s) 851 bytes 2 Dir(s) 24,485,937,152 bytes free c:\@Work\Perl\monks\samira_saber>fc topol1.au topol1.top Comparing files topol1.au and TOPOL1.TOP FC: no differences encountered c:\@Work\Perl\monks\samira_saber>type topol1.top not so SOLITARY SOLUTION SOLVE the problem c:\@Work\Perl\monks\samira_saber>perl process_file_1.pl topol1.top processing 'topol1.top' c:\@Work\Perl\monks\samira_saber>dir Volume in drive C is Acer Volume Serial Number is 9480-355B Directory of c:\@Work\Perl\monks\samira_saber 08/14/2017 05:26 PM <DIR> . 08/14/2017 05:26 PM <DIR> .. 08/14/2017 05:04 PM 759 process_file_1.pl 08/14/2017 04:40 PM 46 topol1.au 08/14/2017 05:26 PM 61 topol1.top 08/14/2017 04:40 PM 46 topol1.top.bak 4 File(s) 912 bytes 2 Dir(s) 24,485,937,152 bytes free c:\@Work\Perl\monks\samira_saber>type topol1.top not so SOL my new stuff SOL my new stuff SOL my new stuff c:\@Work\Perl\monks\samira_saber>fc topol1.au topol1.top.bak Comparing files topol1.au and TOPOL1.TOP.BAK FC: no differences encountered

Please see perlre, perlretut, and perlrequick for (lots and lots of) regex info. See also perlop (update: specifically, the Regexp Quote-Like Operators section) for discussion of the  s/// substitution operator.


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


In reply to Re: sed command with variable in perl by AnomalousMonk
in thread sed command with variable in perl by samira_saber

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.