Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 从sql脚本输出中提取不等于零的值_Bash_Shell_Scripting - Fatal编程技术网

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

Bash 从sql脚本输出中提取不等于零的值,bash,shell,scripting,Bash,Shell,Scripting,我基本上是在两个不同的数据库之间对具有相同名称的表进行行计数 我们的sql脚本如下所示: select (select count(1) from source.abc@remotedb) - (select count(1) from target.bcd) from dual; cat result_of_sql | grep -v '^$' | paste - - - | awk -F"\t" '$3!=0{print $1}' 我们有将近2000个类似于上面的脚本 输出如下所示: s

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

我们的sql脚本如下所示:

select (select count(1) from source.abc@remotedb) - (select count(1) from target.bcd) from dual;
cat result_of_sql | grep -v '^$' | paste - - - | awk -F"\t" '$3!=0{print $1}'
我们有将近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
-B1:匹配行前面的一行可能类似

egrep -B3 '^\-*[1-9]+$' fileinput > final_result.txt
我想这样做:

select (select count(1) from source.abc@remotedb) - (select count(1) from target.bcd) from dual;
cat result_of_sql | grep -v '^$' | paste - - - | awk -F"\t" '$3!=0{print $1}'
在哪里

  • grep-v'^$'
    去掉空行
  • 粘贴----
    使用制表符将每3行聚合为1行
  • awk-F“\t””$3=0{print$1}'
    awk magic:打印预期结果