Chinaunix首页 | 论坛 | 博客
  • 博客访问: 927826
  • 博文数量: 146
  • 博客积分: 3321
  • 博客等级: 中校
  • 技术积分: 1523
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-29 10:32
文章分类

全部博文(146)

文章存档

2014年(2)

2013年(5)

2012年(4)

2011年(6)

2010年(30)

2009年(75)

2008年(24)

分类:

2009-07-21 11:43:33

   刚刚在cu上看到这样一个帖子,比较感兴趣,先列在这里。

    知道文件中的一行 含有“-d”或"“--dst-range" 但是不能确定它在一行中的哪一个域, 我想把“-d”域或“--dst-range ” 域后边的域打印出来!

file:
iptables -A APP -m set --set app10000 src -p tcp -m multiport --dports 1,2,3,4,5,6,7,8  -d 1.1.1.1 -m time  --weektime  111111100002359 -j  DROP
iptables -A APP -m set --set app10000 src -p tcp -m multiport  -d 1.1.1.1 -m time  --weektime  111111100002359 -j  DROP
iptables -A APP -m set --set app10000 -d 1.1.1.1 -m time  --weektime  111111100002359 -j  DROP
iptables -A APP -m set --set app10002 src -p tcp -m multiport --dports  -m iprange --dst-range 2.2.2.2-3 -m time  --weektime
iptables -A APP -m set --set app10000 src -p tcp -m multiport --dports 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16  -m iprange --dst-range 2.2.2.2-3 -m time  --weektime
iptables -A APP -m set --set app10002 src -p tcp -m multiport --dports 1,2,3,4,5,6,7,8  -d 1.1.1.1 -m time  --weektime  111111100002359 -j  DROP

请不要以“-d” 或“--dst-range ”为分隔符,应为选行还有一些别的条件!

实现方法:
1》
along@along-laptop:~/code/shell/shell$ grep -oP '(?<=-d\s)[^\s]+|(?<=--dst-range\s)[^\s]+' file
1.1.1.1
1.1.1.1
1.1.1.1
2.2.2.2-3
2.2.2.2-3
1.1.1.1
2》
along@along-laptop:~/code/shell/shell$ awk '{for(i=1;i<=NF;i++)if($i=="-d"||$i=="--dst-range")printf $(i+1)"";print ""}' file
1.1.1.1
1.1.1.1
1.1.1.1
2.2.2.2-3
2.2.2.2-3
1.1.1.1

第二种方法很容易可以看出来。第一种方法还有点迷糊,不过现写在这里。

-P, --perl-regexp
              Interpret PATTERN as a Perl regular expression.  This is highly experimental and grep -P may warn
              of unimplemented features.
 -o, --only-matching
              Print  only  the  matched (non-empty) parts of a matching line, with each such part on a separate
              output line.


阅读(2677) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~