Re: Need help for import of text file

Поиск
Список
Период
Сортировка
От Peter Bex
Тема Re: Need help for import of text file
Дата
Msg-id 20121216181252.GU4254@frohike.homeunix.org
обсуждение исходный текст
Ответ на Re: Need help for import of text file  (Andreas <maps.on@gmx.net>)
Ответы Re: Need help for import of text file
Список 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 по дате отправления:

Предыдущее
От: Peter Bex
Дата:
Сообщение: Re: Authenticate with hash instead of plaintext password?
Следующее
От: Tom Lane
Дата:
Сообщение: Re: Default timezone changes in 9.1