Re: Need help for import of text file - Mailing list pgsql-general

From Peter Bex
Subject Re: Need help for import of text file
Date
Msg-id 20121216181252.GU4254@frohike.homeunix.org
Whole thread Raw
In response to Re: Need help for import of text file  (Andreas <maps.on@gmx.net>)
Responses Re: Need help for import of text file  (Steve Clark <sclark@netwolves.com>)
List 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


pgsql-general by date:

Previous
From: Peter Bex
Date:
Subject: Re: Authenticate with hash instead of plaintext password?
Next
From: Tom Lane
Date:
Subject: Re: Default timezone changes in 9.1