replace ever 80th character in a string with a quote

J

jason

running under HPUX.

Perl Newbie here.

I have a file with one record that can be very wide, possibly 5000+
bytes wide.

I need to replace every 80th byte in that record with a double quote.
I have the below script, with a failed attempt to replace x number of
spaces with the quote, but that solution will not work upon further
inspection of the data.

#!/usr/bin/perl
strict;
use warnings;
open RIN,"<x1" or die "ERROR!";
open ROUT,">x2" or die "ERROR!";
while (<RIN>) {
$_=~s/ / "/g;
print ROUT ($_);
}
print ROUT ("\"");
close (RIN);
close (ROUT);

Many thanks for any help or information.
 
G

Gunnar Hjalmarsson

I have a file with one record that can be very wide, possibly 5000+
bytes wide.

I need to replace every 80th byte in that record with a double
quote.

s/(.{79})./$1"/g;
 
S

Scott W Gifford

running under HPUX.

Perl Newbie here.

I have a file with one record that can be very wide, possibly 5000+
bytes wide.

I need to replace every 80th byte in that record with a double quote.

Something like this is one way to do the substitution without a regexp:

use constant WIDTH => 80;
my $s = "0123456789" x 100;

foreach my $i (1..length($s)/WIDTH)
{
substr($s,$i*WIDTH-1,1)=q{'};
}

print $s,"\n";

----ScottG.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top