Re: Need help for import of text file

From: Peter Bex <Peter(dot)Bex(at)xs4all(dot)nl>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Need help for import of text file
Date: 2012-12-16 18:12:52
Message-ID: 20121216181252.GU4254@frohike.homeunix.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Sun, Dec 16, 2012 at 06:48:35PM +0100, Andreas wrote:
> With sed as startingpoint I figured it out.
> Those 3 steps make the input files consumable for COPY
>
> 1. dos2unix
> 2. sed -i 's/[ \t]*$//'
> 3. sed -i 's/ / /g'

You can reduce this to one invocation by separating the commands
by a semicolon (or by passing multiple -e flags)

sed -i 's/[ \t]*$//;s/ / /g'

> The input files get created by a simple windows batch where I can't
> change anything.
> It uses echo to attach a line of 4 parameters to those textfiles.
>
> How would you manage if one or more of those parameters contained blanks
> in some cases?
> This doesn't appear, yet. But I consider this as luck. :}
>
> The real column formats are ( TEXT, TEXT, DATE, TIME ).

Well, that's a bit trickier and my sed skills are rather rusty.
I'd probably use awk for these more complex tasks:

awk '/\(.*\)/ { gsub(/ +/, " "); } { print $0 }'

The "gsub" command acts like sed's "s" command with the "g" modifier.
By prefixing the block with the gsub command with a regex, it only
acts on that regex. The regex in this example only looks for an opening
and a closing paren anywhere on the line; you might need to tweak it
to more closely match your case. Alternatively, you could implement
a counter that skips the four lines (which can be done with both sed
and awk).

If it gets more complex than this, you can always write a proper
program in a "real" language to do it. This can be easier to maintain.

Cheers,
Peter
--
http://sjamaan.ath.cx
--
"The process of preparing programs for a digital computer
is especially attractive, not only because it can be economically
and scientifically rewarding, but also because it can be an aesthetic
experience much like composing poetry or music."
-- Donald Knuth

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2012-12-16 18:16:55 Re: Default timezone changes in 9.1
Previous Message Peter Bex 2012-12-16 18:03:42 Re: Authenticate with hash instead of plaintext password?