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

时间:2018-04-11 16:38:30

标签: bash shell scripting

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

我们的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

3 个答案:

答案 0 :(得分:1)

grep -E -B1 '\-{0,1}[1-9][0-9]*' fileinput > final_result.txt

-B1:匹配行之前的一行

答案 1 :(得分:0)

也许像

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

答案 2 :(得分:0)

我会这样做:

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

哪里

  • grep -v '^$'摆脱空行
  • paste - - -将每3行聚合为1个带标签
  • awk -F"\t" '$3!=0{print $1}' awk magic:打印预期结果。