有什么方法可以让psql通过\0 (又名NUL )将字段和记录分开吗?这是将任意数据传递给Bash脚本的唯一方法。
基于马修·伍德的回答,我希望在新初始化的数据库中打印更多的1:
declare -i count=0
echo "\pset recordsep '\000'
\f '\000'
select typname from pg_type" | \
sudo -iu postgres psql --no-align --quiet --tuples-only -d dbname -U username | while IFS= read -r -d ''
do
#echo "$REPLY"
let count++
done
if [ -n "$REPLY" ]
then
#echo "$REPLY"
let count++
fi
echo $count解决方案:Iff SELECT结果是唯一的,您可以使用此解决方案一次处理一个:
next_record() {
psql --no-align --quiet --tuples-only -d dbname -U username <<SQL
SELECT colname
FROM tablename
WHERE colname > '${1}'
ORDER BY colname
LIMIT 1
SQL
}
last_col=
while true
do
colx="$(next_record "$last_col"; printf x)"
if [ "$colx" = x ]
then
exit
fi
col="${colx%$'\nx'}" # The extra \n character is from psql
# Do your thing here
col_escaped="${col//"'"/''}" # Double single quotes
col_escaped="${col_escaped//\\/\\\\}" # Double backslashes
last_col="$col_escaped"
done发布于 2011-07-30 06:37:40
这是不支持的。psql使用C print函数打印出结果表,而打印零字节在那里不起作用。
Update:现在在PostgreSQL 9.2-to-be (git)中支持这一点。
发布于 2011-07-29 00:08:17
试试这个:
psql --field-separator '\000' --no-align -c '<your query>'编辑:也许不是。但是,它似乎在psql中使用以下命令工作:
\f '\000'
\a发布于 2021-05-07 11:36:39
新版本的psql 支持 --field-separator-zero标志。
https://stackoverflow.com/questions/6857265
复制相似问题