Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

匹配末尾的端口号

提取这个字符串的末尾端口号

2017-04-17 08:16:14 INFO     connecting lh3.googleusercontent.com:443 from 111.111.111.111:26215

我可以用grep awk 做出来

1.grep   echo "2017-04-17 08:16:14 INFO     connecting lh3.googleusercontent.com:443 from 111.111.111.111:26215"| grep -Po  'd+$'
2.ack  echo "2017-04-17 08:16:14 INFO     connecting lh3.googleusercontent.com:443 from 111.111.111.111:26215"| ack -o  'd+$'

为何下面的sed awk 无法做到?

3.sed    echo "2017-04-17 08:16:14 INFO     connecting lh3.googleusercontent.com:443 from 111.111.111.111:26215"| sed  's/(.*)::(d+)$/2/g'
4.awk   echo "2017-04-17 08:16:14 INFO     connecting lh3.googleusercontent.com:443 from 111.111.111.111:26215"| awk '{b=gensub(/^(.+)d+)$/,"2","g";print b}'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. 在awk/sed中, d并不能代表数字, 数字只能用[0-9]来匹配, 所以d匹配都是失败的

  2. awk中有语法错误: gensub括号不成对, 在引用捕获分组时, 需要 \, 不能单单一个, 因为shell层面也要转义一次

  3. sed虽然没有语法错误, 但是匹配样式错了, 多了:
    修改后的答案:

awk:
    echo "2017-04-17 08:16:14 INFO     connecting lh3.googleusercontent.com:443 from 111.111.111.111:26215"| awk '{print gensub(/^.+:([0-9]+)$/,"\1","g")}'
sed:
    echo "2017-04-17 08:16:14 INFO     connecting lh3.googleusercontent.com:443 from 111.111.111.111:26215"| sed -r  's/(.*):([0-9]+)$/2/g'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...