in reply to Replacing spaces

As your fields are fixed width, you should unpack them using a suitable template and then join them to put the tabs in.

In this case, the suitable template char would be 'A' (with an appropriate count), to truncate the trailing spaces in each of your left-justified fields.

Something like this should do the trick:

#! perl -sw use 5.010; use strict; while( <> ) { my @fields = unpack 'A31 A31 A26 A13 A9 A11 A31 A26 A26 A1', $_; say join "\t", @fields; } __END__ c:\test> thisScript.pl yourFile > theNewFile

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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: Replacing spaces
by apachi15 (Initiate) on Apr 07, 2009 at 12:47 UTC
    OK, Thanks for the help- I have never worked with that pack or unpack function. I will have a deeper look into it an give it a try. Martin
      I will have a deeper look into it an give it a try.

      If the sample you posted is representative of your data, the script I posted above should do the job for you.

      That said, if you are regularly working with fixed record length files getting familiar with pack/unpack will save you heaps of time in the long run.


      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.
        Thanks very much -it's working! But don't ask me why, could you do me a favour and comment code for a poor (part time) programmer like me. Thanks Martin