in reply to Text file processing question

Two lines should do the job:

#! perl -slw use strict; print "Name of consumer Subdivision Amount Type of work Phase"; printf "%-14s %14s %7.2f %-14s %-s\n", m[(\S+)\s+(\S+)\s+(\S+)\s+(.+)\s+(\S+)$] while <DATA>; __DATA__ Nancy Location1 600.00 New service Single Stella Location2 1000.00 Replacement Single Julie Location3 400.00 Replacement Three Debbie Location4 800.00 New service Three

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.
RIP PCW It is as I've been saying!(Audio until 20090817)

Replies are listed 'Best First'.
Re^2: Text file processing question
by perl_seeker (Scribe) on Aug 13, 2009 at 12:35 UTC
    :) This is really cool.

    Way to go BrowserUk. Thanks a lot. I now have to get down to the business of understanding the regular expression.

    perl_seeker
      I now have to get down to the business of understanding the regular expression.

      There's not much to it really. Essentially the input file consists of 5 groups of non-whitespace characters interpersed with whitespace.

      The only fly in that ointment is the 4th field which itself contains whitepace, and so prevents trival parsing with split. Luckily, as only one field contains multiple words, we can let the regex engine sort it out for us. Ask it for the first 3 whitespace delimited fields, and the last one. And the fourth field is everything left over.

      You might find it clearer expanded out like this:

      m[ ( \S+ ) \s+ ( \S+ ) \s+ ( \S+ ) \s+ ( .+ ) \s+ ( \S+ ) $ ]x

      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.
        What if more than one field contains multiple words ?

        In fact now i have multiple words in more than one field:
        Nancy Drew Elm Street 600.00 New service Single Stella Barry Pine Avenue 1000.00 Replacement Single Julie Green Palm Street 400.00 Replacement Three Debbie Brown Baker Avenue 800.00 New service Three
        perl_seeker