我想计算特定模式中存在的表中的行数。我已经计算了表的行数,但我想要以下格式的输出:-
Total number of Row Count for table_name is :[value]
我已经准备好了脚本,在其中我可以获取架构中存在的每个表的计数。但我担心的是我想要这样的输出:-
total no. of row count in table_name is [value].
类似这样的事情
#!/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
我想要的结果如下:-
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 :
首先打印计数,然后打印表名称。
最佳答案
常用方法如何使用psql
shell 脚本中的输出。
对于单行输出:
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
block 之外不可用)
这里:
-
-X
psql
选项:不要使用~/.psqlrc
文件以避免不必要的输出 -
copy (<query>) to stdout with (format csv, delimiter e'\\t')
:输出<query>
的结果使用CSV
以 制表符 字符作为分隔符的格式 -
IFS=$'\t'
:临时设置默认的shell分隔符为Tab字符
(这里是字符串常量的特殊语法,对比echo "a\tb"; echo -e "a\tb"; echo $'a\tb'
) -
read a b
:读取由$IFS
分隔的输入进入变量 -
<<<
:使用后面的值作为上一个命令的输入 - ...等
关于linux - 如何打印特定模式下表中的总行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57886259/