用于清理 IP 的 AWK 间隔表达式

用于清理 IP 的 AWK 间隔表达式

感谢您在线提供的大量条目和手册。

它确实有效,但是,它足够有效吗?
你同意吗?
{据我所知,读取日志 +- 5k 条的速度很快}

文件“myAwk3.txt”中的源数据:

type,Srv-1,2016-04-16 23:59:02,Name1,1234567890abcdef123456789bbbbb00,(10.90.178.4:1004)
type,Srv-1,2016-04-16 23:59:24,<N@me> (-) [==]1 23,1234567890abcdef123456789bbbbb22,(192.10.196.74:65005)

现在有效的代码:

awk --re-interval -F"," 'BEGIN{OFS=",";}{ 
patPort=":[0-9]{1,5}";              # Will find ":1" to ":65xxx"
IpPort=$6                           # Field needed, (192.10.196.74:65005)
IpOnly=gensub(patPort, "",1,$6);    # Remove pattern
IpOnly=gensub(/[(]/, "",1,IpOnly);  # Remove "(" , With sed: sed 's/[()]//g' 
IpOnly=gensub(/[)]/, "",1,IpOnly);  # Remove ")"
print IpPort, IpOnly;               # Show old and new data
} ' myAwk3.txt

结果:

(10.90.178.4:1004),10.90.178.4
(192.10.196.74:65005),192.10.196.74

答案1

我建议消除正则表达式匹配和替换开销:

awk '
    BEGIN { FS = OFS = "," }
    { 
        split($6, a, ":");
        print substr(a[1], 2);
    }
' myAwk3.txt

相关内容