gensub()函数
对于
Thu Jan 12 14:36:25 2017 TCP from 192.168.222.128:9300 to 112.168.222.129:46471 44 bytes,
句子,把最后一个:变成空白
echo "Thu Jan 12 14:36:25 2017 TCP from 192.168.222.128:9300 to 112.168.222.129:46471 44 bytes," | awk ‘{print gensub (":"," ",4)}‘
Thu Jan 12 14:36:25 2017 TCP from 192.168.222.128:9300 to 112.168.222.129 46471 44 bytes,
":"匹配冒号," "把冒号变成空白,4是这一句里面的第4个冒号,如果是g或者G的话,就是全局替换。gensub有4项目,最后一个可以省略,默认省略的是$0,如果写成这样awk ‘{print gensub (":"," ",4,$0)}‘,效果相同。
echo "Thu Jan 12 14:36:25 2017 TCP from 192.168.222.128:9300 to 112.168.222.129:46471 44 bytes," | awk ‘{print gensub (":"," ",2,$4)}‘
14:36 25
这就是以空白为分隔符,对第4个字段(14:36:25)里面,替换第2个冒号为空白。
echo "aaa bbb" | awk ‘{print gensub(/(.+) (.+)/,"\\2 \\1","g")}‘
bbb aaa
这就完成了替换。
echo "xaax xbx xxx:xaax xbx xxx" | awk -F: -vOFS=":" ‘{$2=gensub(/x([^x]+)x/,"\\
1YY",2,$2)}1‘
xaax xbx xxx:xaax bYY xxx
这个比较复杂。冒号分列,$2对后半操作,2是针对/x([^x]+)x/匹配第二次,()是匹配的b,被\\
1调用,\要写2次,然后赋值给$2,最后的1相当于;print
gsub()函数
echo "Thu Jan 12 14:36:25 2017 TCP from 192.168.222.128:9300 to 112.168.222.129:46471 44 bytes," | awk ‘gsub (":","!",$4)‘
Thu Jan 12 14!36!25 2017 TCP from 192.168.222.128:9300 to 112.168.222.129:46471 44 bytes,
和gensub()函数很相近,但是如果按照它的写法,得出的结论却不一样
echo "Thu Jan 12 14:36:25 2017 TCP from 192.168.222.128:9300 to 112.168.222.129:46471 44 bytes," | awk ‘{print gsub (":","")}‘
4
结果是4,等于是返回冒号出现的次数。
另外就是不能处理符号“.”,被当成任义的意思了,要加上两个\\
echo "Thu Jan 12 14:36:25 2017 TCP from 192.168.222.128:9300 to 112.168.222.129:46471 44 bytes," | awk ‘gsub ("\\.","")‘
Thu Jan 12 14:36:25 2017 TCP from 192168222128:9300 to 112168222129:46471 44 bytes,
sub()函数
echo "11111 22222 44444" | awk ‘sub(/22222/,"33333")‘
11111 33333 44444
echo "11111 22222 44444 22222" | awk ‘sub(22222,33333)‘
11111 33333 44444 22222
第二次出现的22222不被替换,只替换第一个。也可以指定域,sub(22222,33333,$0)
本文出自 “天才没有那1%是万万不行的” 博客,请务必保留此出处http://xushen.blog.51cto.com/1673219/1894153
原文地址:http://xushen.blog.51cto.com/1673219/1894153