Is there a correct way to parse output in Bash from a command?

妖精的绣舞 提交于 2019-12-12 04:25:34

问题


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

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

Edit: 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

In that, 9 is the ID I want.


回答1:


With 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/'

Output:

9


来源:https://stackoverflow.com/questions/43329771/is-there-a-correct-way-to-parse-output-in-bash-from-a-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!