http://www.perlmonks.org?node_id=999325


in reply to Re: Increaing a field size for a record
in thread Increaing a field size for a record

There's no magic that would allow expand field in place.

What? No magic? But this is Perl — of course there’s magic!

#! perl use strict; use warnings; use Tie::File; my $filename = 'data.txt'; tie my @lines, 'Tie::File', $filename or die "Cannot tie file '$filen +ame' to array: $!"; for (my $i = 0; $i < @lines; ++$i) { my @fields = split ' ', $lines[$i]; $fields[1] .= ' ' x (50 - length $fields[1]); $lines[$i] = join(' ', @fields); } untie @lines;

Test file “data.txt” before running the script:

DSE 1232123456 ABCDEF96DS0 20100722 20120827KJHLK LKEDX 058305574 IAC +8 S73 WERTYU DSE 987 ABCDEF96DS0 20100722 20120827KJHLK LKEDX 058305574 IAC 8 S73 W +ERTYU DSE 12321234567890 ABCDEF96DS0 20100722 20120827KJHLK LKEDX 058305574 +IAC 8 S73 WERTYU

and after:

DSE 1232123456 ABCDEF96DS0 201 +00722 20120827KJHLK LKEDX 058305574 IAC 8 S73 WERTYU DSE 987 ABCDEF96DS0 201 +00722 20120827KJHLK LKEDX 058305574 IAC 8 S73 WERTYU DSE 12321234567890 ABCDEF96DS0 201 +00722 20120827KJHLK LKEDX 058305574 IAC 8 S73 WERTYU

;-)

Athanasius <°(((><contra mundum