[20210924]awk奇怪的输出2.txt
--//上班测试再次遇到奇怪的问题,自己记录一下.
1.环境:
SCOTT@book> @ ver1
PORT_STRING VERSION BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx 11.2.0.4.0 Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
2.测试:
$ sqlplus -s -l scott/book <<< "select empno,deptno,sal,comm from emp;"
EMPNO DEPTNO SAL COMM
---------- ---------- ---------- ----------
7369 20 800
7499 30 1600 300
7521 30 1250 500
7566 20 2975
7654 30 1250 1400
7698 30 2850
7782 10 2450
7788 20 3000
7839 10 5000
7844 30 1500 0
7876 20 1100
7900 30 950
7902 20 3000
7934 10 1300
14 rows selected.
$ sqlplus -s -l scott/book <<< "select empno,deptno,sal,comm from emp;" | awk '$4 >=0 {print $0 }'
EMPNO DEPTNO SAL COMM
7499 30 1600 300
7521 30 1250 500
7654 30 1250 1400
7844 30 1500 0
--//输出header是对的,awk当作字符串比较.
$ sqlplus -s -l scott/book <<< "select empno,deptno,sal,comm from emp;" | awk '$4*1 >=0 {print $0 }'
EMPNO DEPTNO SAL COMM
---------- ---------- ---------- ----------
7369 20 800
7499 30 1600 300
7521 30 1250 500
7566 20 2975
7654 30 1250 1400
7698 30 2850
7782 10 2450
7788 20 3000
7839 10 5000
7844 30 1500 0
7876 20 1100
7900 30 950
7902 20 3000
7934 10 1300
14 rows selected.
--//当字段不存在时$4*1 当作0 ,这样符合条件.看如下输出.
$ sqlplus -s -l scott/book <<< "select empno,deptno,sal,comm from emp;" | awk '$4*1 >0 {print $0 }'
7499 30 1600 300
7521 30 1250 500
7654 30 1250 1400
--//也就是awk把位置参数不存在时数字当作0来处理了.如何解决呢?
--//我自己也没有想到什么好方法改写如下:
$ sqlplus -s -l scott/book <<< "select empno,deptno,sal,comm from emp;" | awk '$4+0 >0 || $4=='0' {print $0 }'
7499 30 1600 300
7521 30 1250 500
7654 30 1250 1400
7844 30 1500 0
--//重点了解这些细节.