All pastes #24878 Raw Edit

getridof.sh

public shellscript v1 · immutable
#24878 ·published 2005-10-07 20:38 UTC
rendered paste body
#!/bin/sh# This script deletes tables from a PostgreSQL pg_dump plain# text dump.  This is useful for getting smaller backups.## Usage: getridof.sh <backup bzip> <table name># Check that arguments are giventables="usr* x3d*"            if [ "$1" = "" ]; then        echo "Error: no backup file given"        exit    fi# Extract the backup to a working SQL scriptcp "$1" ${1}_to_norm.sql.gzgunzip < ${1}_to_norm.sql.gz > working.sqlarray=($(egrep '^(usr|x3d)' < working.sql))    for table in ${array[@]}    do# Find the line before the table's COPY output begins        START=`grep -n "^COPY $table " working.sql | sed -e 's/:.*//'`        ((START--))#Find line number at which COPY ends#Trying to use awk        END=`grep -n "^\\\\\\." working.sql | awk -v START=$START '{if ($1 > START) print $1}' | sed -e 's/:.*//' | head -1`        ((END++))# Remove all contents of the working file containing the COPY command        sed -i -e $START,$END d working.sql    done# Rename the working file to the table namemv working.sql "${1}_normalized.sql"gzip "${1}_normalized.sql" > "${1}_normalized.sql.gz"# Remove temporary filerm working.sql-erm ${1}_to_norm.sql.gz