有没有正确的方法来解析命令中Bash的输出? [英] Is there a correct way to parse output in Bash from a command?
问题描述
我正在使用psql
运行一些命令,其中最后一个类似于:select max(id) from tablename
.我正在尝试获取插入的最后一行的ID.在这种情况下,它将返回正确的值(7)
.
I'm using psql
to run a few commands, where the last one is something similar to: select max(id) from tablename
. I'm trying to get the id of the last row inserted. It returns the correct value (7)
in this scenario.
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
However, I'm trying to parse it and have no idea what to do... Is there a way to limit the output to just the value of 7
? If not, how do I grab just the number? There is the possibility of this becoming a rather large number as well, like 10,000
我添加了2个选项-qt
,并将输出降低到:
I added 2 options -qt
and got the output down to:
Begin Informatica Script 9 Redshift Delta copy completed at: 04/10/17 17:53:53 END
在这种情况下,9
是我想要的ID.
In that, 9
is the ID I want.
推荐答案
使用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.*/\1/'
输出:
9
这篇关于有没有正确的方法来解析命令中Bash的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!