#!/bin/bash -
#===============================================================================
#
#          FILE:  sqlcopysearch.sh
#
#         USAGE:  ./sqlcopysearch.sh <SQL file> <Table name> [Search string]
#
#   DESCRIPTION:  Dump a COPY from a .sql file
#
#  REQUIREMENTS:  postgres
#        AUTHOR: rickz (Rick Zhang), xlrickz@gmail.com
#       COMPANY: X-LEGEND Entertainment Corp.
#       CREATED: Mon Dec 20 17:23:35 CST 2010
#      REVISION:  1.0
#===============================================================================

#set -o nounset                              # Treat unset variables as an error
#set -m                                      # Enable job control

[ "$1" == "" ] && exec echo "Usage: $0 <SQL file> <Table name> [Search string]

Search string could be:
20101211
or
20101211,206784

eg.
\$ $0 ElfDB.sql gold_log 20101211,206784
20101211        206784  -275300 0       0       0       0       0       0       0       0       0       0


If you want to import a COPY from a SQL backup file, you can:
(echo 'DELETE FROM gold_log;' ; sqlcopysearch ElfDB.sql gold_log) | psql ElfDB postgres
                   ^^^^^^^^                   ^^^^^^^^^ ^^^^^^^^         ^^^^^ ^^^^^^^^
                   Table                      SQL file  Table            DB    psql user

With zcat:
zcat ElfDB_1104201742.gz | sqlcopysearch - player_characters Rickz
"


if [ "$3" == "" ] ;then

        sed -n "/COPY $2 /,/^$/p" "$1"

else
        SEARCHSTRING="$(sed 's/,/\[\[:blank:\]\]/g' <<< "$3")"
        sed -n "/COPY $2 /,/^$/p" "$1" | grep "$SEARCHSTRING"

fi                                                                 

