gawk中使用位运算和用例

来源:互联网 发布:武汉网页美工培训 编辑:程序博客网 时间:2024/06/06 05:21

(@转载请注明出处:http://blog.csdn.net/cmatch

 

有时候需要在awk中使用位运算,来解决有些字段的解析、判断。但是一般的awk不支持此类运算,幸运的是gawk中可以支持此类运算。下面是手册中的说明:

 

Bit Manipulations Functions
Starting with version 3.1 of gawk, the following bit manipulation func-
tions are available. They work by converting double-precision floating
point values to unsigned long integers, doing the operation, and then
converting the result back to floating point. The functions are:

and(v1, v2) Return the bitwise AND of the values provided by v1
and v2.

compl(val) Return the bitwise complement of val.

lshift(val, count) Return the value of val, shifted left by count
bits.

or(v1, v2) Return the bitwise OR of the values provided by v1
and v2.

rshift(val, count) Return the value of val, shifted right by count
bits.

xor(v1, v2) Return the bitwise XOR of the values provided by v1
and v2.

 

 

举例:

awk '$2=="A" && $3=="P" && and($4,0x00001)==1' /tmp/adzone.xml > tmp

 

原创粉丝点击