是否有正确的方法来解析命令 Bash 中的输出?
Is there a correct way to parse output in Bash from a command?
我正在使用 psql
到 运行 几个命令,其中最后一个类似于:select max(id) from tablename
。我正在尝试获取插入的最后一行的 ID。在这种情况下,它 returns 是正确的值 (7)
。
Begin Informatica Script SET INSERT 0 1 max ----- 7 (1 row) Redshift Delta copy completed at: 04/10/17 17:45:21 END
但是,我正在尝试解析它,但不知道该怎么做...有没有办法将输出限制为 7
的值?如果没有,我如何只获取号码?这也有可能成为一个相当大的数字,比如 10,000
编辑:我添加了 2 个选项 -qt
并将输出缩减为:
Begin Informatica Script 9 Redshift Delta copy completed at: 04/10/17 17:53:53 END
其中9
就是我要的ID
使用 GNU sed:
echo 'Begin Informatica Script 9 Redshift Delta copy completed at: 04/10/17 17:53:53 END' | sed -r 's/.*Begin Informatica Script ([0-9,]+) Redshift Delta.*//'
输出:
9
我正在使用 psql
到 运行 几个命令,其中最后一个类似于:select max(id) from tablename
。我正在尝试获取插入的最后一行的 ID。在这种情况下,它 returns 是正确的值 (7)
。
Begin Informatica Script SET INSERT 0 1 max ----- 7 (1 row) Redshift Delta copy completed at: 04/10/17 17:45:21 END
但是,我正在尝试解析它,但不知道该怎么做...有没有办法将输出限制为 7
的值?如果没有,我如何只获取号码?这也有可能成为一个相当大的数字,比如 10,000
编辑:我添加了 2 个选项 -qt
并将输出缩减为:
Begin Informatica Script 9 Redshift Delta copy completed at: 04/10/17 17:53:53 END
其中9
就是我要的ID
使用 GNU sed:
echo 'Begin Informatica Script 9 Redshift Delta copy completed at: 04/10/17 17:53:53 END' | sed -r 's/.*Begin Informatica Script ([0-9,]+) Redshift Delta.*//'
输出:
9