Hi, I need a script to find and replace some stuff from a certain part of a text file (after, say, character 279) and then put the lines back together. i've done this, but I seem to belosing a column of text... there is one character missing in the new files that was in the old ones, but I can't figure out why (or where... though I would guess it's at 279, where by substring stiching goes on).

here is the code

#!/usr/bin/perl -w @infile = ('/mnt/ide0/home/wrml/ReturnA/KCRETA60.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA61.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA62.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA63.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA64.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA65.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA66.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA67.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA68.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA69.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA70.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA71.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA72.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA73.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA74.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA75.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA76.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA77.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA78.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA79.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA80.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA81.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA82.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA83.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA84.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA85.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA86.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA87.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA88.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA89.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA98.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA99.DAT', '/mnt/ide0/home/wrml/ReturnA/KCRETA00.DAT', '/mnt/ide0/home/wrml/ReturnA/KEN90.DAT', '/mnt/ide0/home/wrml/ReturnA/KEN91.DAT', '/mnt/ide0/home/wrml/ReturnA/KEN92.DAT', '/mnt/ide0/home/wrml/ReturnA/KEN93.DAT', '/mnt/ide0/home/wrml/ReturnA/KEN94.DAT', '/mnt/ide0/home/wrml/ReturnA/KEN95.DAT', '/mnt/ide0/home/wrml/ReturnA/KEN96.DAT', ); $outpath = '/mnt/ide0/home/wrml/FormatReturnA/'; use File::Basename 'basename'; %substitute = ( '0000I' => '00000', '0000J' => '-0001', '0000K' => '-0002', '0000L' => '-0003', '0000M' => '-0004', '0000N' => '-0005', '0000O' => '-0006', '0000P' => '-0007', '0000Q' => '-0008', '0000R' => '-0009', '0001I' => '-0010', '0001J' => '-0011', '0001K' => '-0012', '0001L' => '-0013' ); $alt = join '|', keys(%substitute); $regex = qr/($alt)/; open (COUNT, '>', '/mnt/ide0/home/wrml/ReturnA/Counter.txt'); foreach $file (@infile){ open(IN, '<', "$file") or warn $! and next; open(OUT, '>', $outpath.basename($file)) or warn $! and next; while (<IN>) { $Line = $_; $DataLine = substr($Line, 279); $DataLine =~ s/$regex/$substitute{$1}/g; $FirstLine = substr($Line, 0, 279); substr($FirstLine, -1, 1) = $DataLine; print OUT $FirstLine; $length = length($FirstLine); if ($length != 7383) { print COUNT "you have a problem in file $file. The lin +e is $length\n" } } }
I know I'm losing something because I ran a file to count the lines in each and the original is one character longer. that is, all the lines are 7387 long in the original and 1386 in the new... doing a simple 'length($TheLine)'. what have I done?

please help.

Janitored by Arunbear - replaced pre tags with code tags, to prevent distortion of site layout and allow code extraction.


In reply to Lost Column by wrml

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.