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

标签 linux bash postgresql postgresql-9.5

我想计算特定模式中存在的表中的行数。我已经计算了表的行数,但我想要以下格式的输出:-

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/

相关文章:

bash - 找出脚本需要的 BASH 版本

SQL 改变类型

postgresql - 使用 PL/pgSQL 函数将具有多个值的列动态添加到任何表

linux - 可执行文件中的时间限制

bash - 带有 OR 逻辑的 Grep 命令

linux - fork: retry: 资源暂时不可用

bash - 如果任何功能并行失败,请停止 bash

sql - 从映射行 SQL 中选择主数据库

regex - 正则表达式 - Linux - 之间,但不包括

ruby - 无法安装 selenium-webdriver