繁体   English   中英

从sql脚本输出中提取不等于零的值

[英]Extract values from sql script output which are not equal to zero

我基本上是在2个不同的数据库之间对具有相同名称的表进行行计数。

我们的sql脚本是这样的:

select (select count(1) from source.abc@remotedb) - (select count(1) from target.bcd) from dual;

我们有将近2000个与上述相似的脚本。

输出如下:

select count(1) from source.abc@remotedb) - (select count(1) from target.abc
----------------------------------------------------------------------------
0

select count(1) from source.opo@remotedb) - (select count(1) from target.opo
----------------------------------------------------------------------------
26

select count(1) from source.asd@remotedb) - (select count(1) from target.asd
----------------------------------------------------------------------------
-95

现在使用bash / shell脚本,我想将输出仅打印到数值不等于0的那三行的单独文件中。

例:

$ cat final_result.txt

select count(1) from source.opo@remotedb) - (select count(1) from target.opo
----------------------------------------------------------------------------
26

select count(1) from source.asd@remotedb) - (select count(1) from target.asd
----------------------------------------------------------------------------
-95
grep -E -B1 '\-{0,1}[1-9][0-9]*' fileinput > final_result.txt

-B1:匹配行之前一行

也许像

egrep -B3 '^\-*[1-9]+$' fileinput > final_result.txt

我会这样:

cat result_of_sql | grep -v '^$' | paste - - - | awk -F"\t" '$3!=0{print $1}'

哪里

  • grep -v '^$'删除空行
  • paste - - -每三行用制表符聚合为1
  • awk -F"\\t" '$3!=0{print $1}' awk魔术:打印预期结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM