Perl - Replacing  a record at a specific position

Posted on 2012-09-07
Last Modified: 2012-09-08

Using Perl, how could you replace a record at a specific position of a fixed length file?    Also, need to do multiple replacement at different positions for the same record.

For example, the file contains the following record:

-->  Need to change THIS first then THIS second ...<rest of line, 700 chars total>

Note:  1st "THIS" is at positions 16-19 and 2nd "THIS" is at position 32-35.


Replace position 16 to 19 (len = 4), "THIS", with "XXXX"
Replace position 32 to 35 (len = 4), "THIS", with "YYY" << len different


--> Need to change XXXX first then YYYS second ...<rest of line, 700 chars total>

Note:  The 2nd replacement replaced "THIS" with "YYYS"; 'S' was kept and the records length was not changed, contained 700 chars (same as original)

Thank you

I need to change several
I need to modify a fixed length file, at several positions o
Question by:tkir
    LVL 84

    Accepted Solution

    open F,"+<file" or die $!;
    seek F,15,0;
    print F "XXXX";
    seek F,31,0;
    print F "YYY";
    close F;
    LVL 2

    Assisted Solution

    try something like:

      my($line) = $_;
      substr($line, 16, 4) = "XXXX";
      substr($line, 32, 3) = "YYY";
      print "$line\n";
    LVL 2

    Assisted Solution

    In this case, there is no more magic in Perl than in any other language.
    While files are not too big, just read a file, change data, and write it back:
    @subs=([19,4,'XXXX'], [32,4,'YYY']);
    open F, '<+:raw', $file or die$!;
    read F, $data, -s F;
    for $sub(@subs)  {
       substr($data, $sub->[0],$sub->[1])=$sub->[2];
    open F, '>:raw', $file or die$!; 
    print F $data;

    Open in new window

    Otherwise, you'll need to read the file piece-meal and write changed data into another file.
    Of course, if replaced and replacing data have the same length, you can change the file
    in place (see the first answer by ozo).

    Author Closing Comment

    Thank you!!

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now