Re: Increaing a field size for a record
by MidLifeXis (Monsignor) on Oct 16, 2012 at 12:40 UTC
|
(Note: please update your post to include <code>...</code> tags around any code or data. A well-formatted post will increase the likelihood of receiving a useful response)
You would need to do something along the lines of:
- break each line apart into fields
- reprint the fields in the new format
Some things you may want to look at might be split, perlre, say, and printf.
| [reply] [d/l] |
Re: Increaing a field size for a record
by zwon (Abbot) on Oct 16, 2012 at 12:42 UTC
|
That's fairly simple. You just read the file, parse the records, and output them into a new file in a desired fashion. Then rename file and that's it. There's no magic that would allow expand field in place.
| [reply] |
|
#! 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
| [reply] [d/l] [select] |
|
That's a funny approach, but is not very efficient, every time you're assigning to $line[$i] it rewrites all the file starting from the record $i.
Update: More efficient would be to use File::Slurp:
use strict;
use warnings;
use File::Slurp qw(edit_file_lines);
sub fix {
my @fields = split / +/;
$fields[1] = sprintf "%-50s", $fields[1];
$_ = join ' ', @fields;
}
edit_file_lines \&fix, $file_name;
The problem is that it doesn't preserve the width of the other fields | [reply] [d/l] [select] |
|
| [reply] [d/l] |
|
|
|
Thanks Athanasius! That was really halpful
I got a problem, the positions are not space separated, If I insert 39 spaces from postion 15 in all records will help.. how can I do that ?
| [reply] |
|
|
Re: Increaing a field size for a record
by Kenosis (Priest) on Oct 16, 2012 at 17:07 UTC
|
use strict;
use warnings;
use File::Slurp qw/read_file write_file/;
write_file 'rij1.txt', map s/^(\S+) (\S+ )/"$1 $2" . ' ' x 39/er,
read_file 'rij1.txt';
Have assumed the second field isn't empty, as it seems that the OP's suggesting it's currently fixed at 11 non-space characters to which 39 spaces are to be added. | [reply] [d/l] |
Re: Increaing a field size for a record
by flexvault (Monsignor) on Oct 16, 2012 at 18:53 UTC
|
my $record = <$fh>; # read the record
substr( $record, 15, 0, " " x 39 ); # insert 39 spaces
This will insert 39 spaces after the 15th character. If your counting starting at 1, test that it is going into the correct location :-)
Good Luck!
"Well done is better than well said." - Benjamin Franklin
| [reply] [d/l] |
|
Thanks!!
But this doesn't seam to be working for me. :(
#!/usr/bin/perl
open FILE, rij1.dat";
my @lines = <FILE>;
foreach my $lin (@lines)
{
my $btw = substr($lin, 15, 0, "1" x 39);
print "$btw";
}
close FILE;
| [reply] |
|
foreach my $lin (@lines) { substr($lin, 15, 0, "1" x 39); print "$lin\
+n"; }
"Well done is better than well said." - Benjamin Franklin
| [reply] [d/l] |
Re: Increaing a field size for a record
by McA (Priest) on Oct 16, 2012 at 12:34 UTC
|
Hi,
are the fields in the record space seperated or at certain positions? A little more infomation about the record format would be helpful.
Best regards
McA
| [reply] |
|
Yeah, the fields are space separated..
| [reply] |
|
Ok, if the fields are seperated exactly by one space and you add spaces at the end of one field you are aware that you change the way fields can be split afterwards? If the fields are seperated by one or more spaces, it doesn't matter if you add some more spaces.
So, please let us know what is it good for. I'm really interested.
Best regards
McA
| [reply] |