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

Hi I have the fixed width flat file having the following data 12345aaaaaaaaaabbbbbbbbbb 12365sssssssssscccccccccc 12365sssss 12367ddddddddddvvvvvvvvvv 12367 vvvvv Here the first column is length 5 second is length 10 third is length 10 if the second or third column exceeds length it is repeated in next line with the first column repeated Now I need the file to be modified in such a way that there is only one row for the data like 12345aaaaaaaaaa bbbbbbbbbb 12365sssssssssssssss cccccccccc 12367dddddddddd vvvvvvvvvvvvvvv changing the lenght of the second and third column to 20 instead of 10. I am trying to write this in perl or shell. Kindly help me..

Replies are listed 'Best First'.
Re: Manipilate the fixed lenght flat file
by kennethk (Abbot) on Apr 06, 2009 at 15:57 UTC

    Please read Writeup Formatting Tips, as it is difficult to understand your intention w/o seeing your input file properly formatted (see the section on <code> tags).

    What have you tried? Why do you want to do this in Perl, and do you have any experience with it? This is, after all a help resource and not a code writing service.

    For some introductory guidance, you'll probably want to take a look at I/O Operators to see how to read/write files and using substr (or pack for the more adventurous) to reformat your strings. You can likely perform all this in a one-liner using the -pi switches (see perlrun), but I'm guessing that's outside your current experience level. If you post some code, or even pseudo-code, I'll be happy to provide pointers and debug assistance (see How (Not) To Ask A Question).

Re: Manipilate the fixed lenght flat file
by BrowserUk (Patriarch) on Apr 06, 2009 at 17:51 UTC

    Based on your sparse spec. this might get you started.

    The two things to note are:

    1. pack & unpack for dealing with fixed width data.
    2. Storing the data from each line until you've looked at the next line to decide whether it's a full line or an extension.
    #! perl -sw use 5.010; use strict; my( $lastID, $lastF1, $lastF2 ) = 0; while( <DATA> ) { chomp; my( $id, $f1, $f2 ) = unpack 'a5 a10 a10', $_; if( $id && $id == $lastID ) { if( substr( $f1, 0, 1 ) eq ' ' ) { ## second field extension $lastF2 .= substr( $f1, 1 ); } else { $lastF1 .= $f1; } } else { say pack 'A5 A20 A20', $lastID, $lastF1, $lastF2 if $lastID; ( $lastID, $lastF1, $lastF2 ) = ( $id, $f1, $f2 ); } } __DATA__ 12345aaaaaaaaaabbbbbbbbbb 12365sssssssssscccccccccc 12365sssss 12367ddddddddddvvvvvvvvvv 12367 vvvvv

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Manipilate the fixed lenght flat file
by metaperl (Curate) on Apr 06, 2009 at 16:34 UTC
    I recommend Parse::FixedLength but there are other CPAN offerings... just search for fixedlength and fixedwidth.
Re: Manipilate the fixed lenght flat file
by wfsp (Abbot) on Apr 06, 2009 at 16:49 UTC
    ...if the second or third column exceeds the length...
    How do you know which column exceeded the length? What would be in the next line after the first column? If only the third column exceeded the length (i.e. not the second) how would the next line represent that?

    Or, what am I missing? :-)