All,

I have file which is of ~1gb, my requirement is to replace the specific lines specified range of characters with the provided string. I am using perl 5.8, below is my code. Due to the interpolation oneliners are not getting the variables vaule.

die "usage: perl $0 file_nm line_nb col_start col_end replacement \n" +unless @ARGV; die "Invalid number of arguments \nusage: perl $0 file_nm line_nb col_ +start col_end replacement \n" if @ARGV ne 5; my ($file_nm,$line_nb,$col_start,$col_end,$string)=@ARGV; chomp($content=`perl -ne "print if $. == ${line_nb}" $file_nm`); #get +the specific line from file chomp ($replacement=`perl -ne "substr($content, $col_start, $col_end, +$string);print $content"`); #get the replaced line #print "New content \n$content \n"; `perl -pi -e "s/$content/$replacement/" $file_nm`; #now substitute the + whole line with the new replaced line

Arguments to the script are File name line number ,in which the replacement should take place replacement start position replacement end position string to replace with Suggestions are welcome

--- Update --- Wrote a perl script finally
use File::Basename; die "usage: perl $0 file_nm line_nb pos_start-pos_end replacement \n" +unless @ARGV; die "Invalid number of arguments \nusage: perl $0 file_nm line_nb pos_ +start pos_end replacement \n" if @ARGV ne 5; my ($file_nm,$line_nb,$pos,$string)=@ARGV; my ($pos_start,$pos_end)=split (/-/,$pos); $file_nm=File::Basename::basename($file_nm); open(READ_HN,"$file_nm") or die "Cant open $file_nm $! \n"; open(WRITE_HN,">${file_nm}.updated") or die "Cant write to ${file_nm}. +updated $! \n"; #chomp($content=`perl -ne "print if $. == ${line_nb}" $file_nm`); while(<READ_HN>) { if ($. == $line_nb) { substr($_, $pos_start, $pos_end, $string); } print WRITE_HN "$_\n"; } close(WRITE_HN); close(READ_HN);

In reply to Replace specific lines specific range of characters. by govindkailas

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.