Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Increaing a field size for a record

by Thomas Kennll (Acolyte)
on Oct 16, 2012 at 12:13 UTC ( [id://999305]=perlquestion: print w/replies, xml ) Need Help??

Thomas Kennll has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I wanted to increase all records field size in a file..

for eg) if i have a file like rij1
bash-2.03$ cat rij1 DSE 1232123456 ABCDEF96DS0 + 20100722 20120827KJHLK LKEDX + 05 +8305574 IAC 8 S73 + + WERTYU bash-2.03$

Currently the 2nd field has 11 characters, ie 1232123456

I want to increase the character size to 50, ie append the remaining with spaces.. result "1232123456(39 spaces) --> Total count should be 50 for this field " can someone help here..

Update: Thank You all for your help!! :)

I have a problem as the field are not necessarily field seperated.. If I could insert 39 spaces from postion 15 or from 15th character on all records will do.. Can anyone help.. Thanks again!!

Replies are listed 'Best First'.
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.

    --MidLifeXis

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.

      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

        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

        Hi Athanasius,

        very interesting inline approach. But is it really necessary to use this "un-perl-ish" ;-) C-construct  for (my $i = 0; $i < @lines; ++$i)? Does it also work with a foreach-loop or is then the inline editing broken?

        Best regards
        McA

        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 ?
Re: Increaing a field size for a record
by Kenosis (Priest) on Oct 16, 2012 at 17:07 UTC

    Here's a substitution option:

    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.

Re: Increaing a field size for a record
by flexvault (Monsignor) on Oct 16, 2012 at 18:53 UTC

    Thomas Kennll,

    A very simple way to insert spaces is:

    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

      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;

        Thomas Kennll,

        That's not what I showed you, please note:

        foreach my $lin (@lines) { substr($lin, 15, 0, "1" x 39); print "$lin\ +n"; }

        "Well done is better than well said." - Benjamin Franklin

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

      Yeah, the fields are space separated..

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://999305]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-26 00:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found