【问题标题】:unable to remove footer for psql ressult in shell script无法删除 pl sql 的页脚导致 shell 脚本
【发布时间】:2026-01-13 18:25:02
【问题描述】:

我有一个 psql 命令,我可以在其中获得结果,但我无法删除页脚 我的命令

sshpass -p 'password' ssh mptios@xx.xx.xxx.xxx  "PGPASSWORD=xxxxx psql -a -h 11.11.111.11 -d TGM_bb_les -U bi_it -t -c \"select count(1) from dwpub.td_bank \" "

我对上述命令的结果:-

select count(1) from dwpub.td_bank
    29

但我需要输出为

29

我已经尝试过这个命令,但仍然无法获得预期的输出

sshpass -p 'password' ssh mptios@xx.xx.xxx.xxx  "PGPASSWORD=xxxxx psql -a -h 11.11.111.11 -d TGM_bb_les -U bi_it --pset\"footer=off\" -c \"select count(1) from dwpub.td_bank \" "

谁能帮我解决这个问题

【问题讨论】:

  • 你试过\pset tuples_only on女巫会禁用页眉和页脚吗?

标签: bash shell scripting psql


【解决方案1】:

使用 grep 或 egrep 仅匹配数字。

sshpass -p 'password' ssh mptios@xx.xx.xxx.xxx  "PGPASSWORD=xxxxx psql -a -h 11.11.111.11 -d TGM_bb_les -U bi_it --pset\"footer=off\" -c \"select count(1) from dwpub.td_bank \" " | grep -Eo '[0-9]+$'

【讨论】:

    【解决方案2】:

    用命令试试:

    sshpass -p 'password' ssh mptios@xx.xx.xxx.xxx  "PGPASSWORD=xxxxx psql -a -h 11.11.111.11 -d TGM_bb_les -U bi_it -t -c \"select count(1) from dwpub.td_bank \" "|tail -1
    

    这将只显示最后一行

    【讨论】: