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

倾然丶 夕夏残阳落幕 提交于 2019-12-12 07:02:18

问题


i want to calculate the number of row counts in tables present in specific schema.i have calculated the row count of the tables but i want output in format:-

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

I have prepared the scripts in which i an getting the count of every table present in the schema. But my concern is i want the output like :-

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

something like this

#!/bin/bash

databasename="$(cat /home/enterprisedb/.bash_profile |grep PGDATABASE|awk '{print $1}'|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

I want result like:-

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

But actually its printing output like:-

 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 :

print the count first then table name.


回答1:


The common approach how to use psql output in shell scripts.

For single row output:

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"

For multi row output:

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"

(Note that the values of $a and $b variables is not available outside of while block)

Here:

  • -X psql option: do not use the ~/.psqlrc file to avoid unnecessary output
  • copy (<query>) to stdout with (format csv, delimiter e'\\t'): output the result of <query> using CSV format with Tab character as delimiter
  • IFS=$'\t': Temporary set the default shell delimiter to Tab character
    (here is special syntax for string constants, compare echo "a\tb"; echo -e "a\tb"; echo $'a\tb')
  • read a b: read input delimited by $IFS into the variable(s)
  • <<<: use the followed value as input to the previous command
  • ... etc


来源:https://stackoverflow.com/questions/57886259/how-to-print-the-total-number-of-row-count-in-tables-in-specific-schema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!