如何打印特定模式中表中的总行数?

How to print the total number of row count in tables in specific schema?

我想计算特定 schema.i 中存在的 table 中的行数,已经计算了 table 中的行数,但我希望以以下格式输出:-

Total number of Row Count for table_name is :[value]

我准备了脚本,我在其中获取架构中存在的每个 table 的计数。但我担心的是我想要这样的输出:-

total no. of row count in table_name is [value].

像这样

#!/bin/bash

databasename="$(cat /home/enterprisedb/.bash_profile |grep PGDATABASE|awk '{print }'|cut -d '=' -f2|cut -d ';' -f1)"
echo "$databasename"

listof_table="select t.table_name from information_schema.tables t where t.table_schema = 'emp_history' and t.table_type = 'BASE TABLE' order by t.table_name;"

listof_schema="select schema_name from information_schema.schemata where schema_name ='emp_history';"

query_count="select 'select count(*) from ' || tablename || ' ;' from pg_tables where schemaname='emp_history';"

var2="$(psql -U enterprisedb -d $databasename -c "$listof_schema" -L /home/enterprisedb/schema_name.log)"

sed -i -e "1,6d" /home/enterprisedb/schema_name.log

final_schema_list="$(cat /home/enterprisedb/schema_name.log| head -1|tr -d "[:blank:]" > /home/enterprisedb/final_list.log)"

count="$(cat /home/enterprisedb/final_list.log)"

echo "$count"

var1="$(psql -U enterprisedb -d $databasename -c "$listof_table" -L /home/enterprisedb/table_name.log)"

sed -i -e "1,6d" -e '$d' /home/enterprisedb/table_name.log

table_name="$(cat /home/enterprisedb/table_name.log |tr -d "[:blank:]" > /home/enterprisedb/table_name_final.log)"

table_name1="$(cat /home/enterprisedb/table_name_final.log )"

final_table_name="$(cat /home/enterprisedb/table_name_final.log|head -n -1 > /home/enterprisedb/final.log)"

table_name_final="$(cat /home/enterprisedb/final.log)"

echo "$table_name_final"
for i in $table_name_final
do
        table_count="$(psql -U enterprisedb -d $databasename -c "select count(*) from "$count"."$i";")"
        echo "Total number of Row Count for "$i" is : "$table_count""

done

我想要这样的结果:-

Total number of Row Count for table_name is :[value]

但实际上它的打印输出是这样的:-

 count
-------
     2
(1 row)

Total number of Row Count for mail_template is :
 count
-------
     1
(1 row)

Total number of Row Count for mail_template_key is :
 count
-------
     7
(1 row)

Total number of Row Count for v_biel_kategorie is :
 count
-------
    40
(1 row)

Total number of Row Count for v_sample_type is :

先打印计数,然后 table 名称。

如何在 shell 脚本中使用 psql 输出的常见方法。

对于单行输出:

IFS=$'\t' read a b <<< $(psql -X -c "copy (select 1, random()) to stdout with (format csv, delimiter e'\t')")
echo "a=$a, b=$b"

对于多行输出:

TEMP_IFS=$IFS
IFS=$'\t'
while read a b; do
    echo "a=$a, b=$b"
done <<< $(psql -X -c "copy (select i, random() from generate_series(1,5) as i) to stdout with (format csv, delimiter e'\t')")
IFS=$TEMP_IFS
echo "a=$a, b=$b"

(注意 $a$b 变量的值在 while 块之外不可用)

此处:

  • -X psql选项:不使用~/.psqlrc文件以避免不必要的输出
  • copy (<query>) to stdout with (format csv, delimiter e'\t'):使用CSV格式输出<query>的结果,Tab字符作为分隔符
  • IFS=$'\t':临时设置默认的shell分隔符为制表符字符
    (这里是字符串常量的特殊语法,比较echo "a\tb"; echo -e "a\tb"; echo $'a\tb')
  • read a b:将由 $IFS 分隔的输入读入变量
  • <<<: 使用后面的值作为前一个命令的输入
  • ...等等