下面的日志可能是什么正则表达式?

时间:2017-03-17 10:47:45

标签: awk sed

我想删除第一个括号中的数字,并保持括号的其余部分完好无损。

Mar 17 00:03:13   %ASA-5-106100: access-list   permitted tcp  10.252.0.165(50811) ->  172.19.26.33(4902) 
Mar 17 00:03:16   %ASA-5-106100: access-list   permitted tcp  10.252.0.166(54563) ->  172.19.26.33(4902) 
Mar 17 00:03:28   %ASA-5-106100: access-list   permitted tcp  10.252.0.222(38071) ->  172.19.26.33(4902) 
Mar 17 00:03:41   %ASA-5-106100: access-list   permitted tcp  10.252.0.222(38074) ->  172.19.26.33(4902) 
Mar 17 00:03:45   %ASA-5-106100: access-list   permitted tcp  10.252.0.221(17868) ->  172.19.26.33(4902) 
Mar 17 00:03:58   %ASA-5-106100: access-list   permitted tcp  10.252.0.166(54572) ->  172.19.26.33(4902) 
Mar 17 00:03:58   %ASA-5-106100: access-list   permitted tcp  10.252.0.166(54573) ->  172.19.26.33(4902) 
Mar 17 00:03:58   %ASA-5-106100: access-list   permitted tcp  10.252.0.166(54574) ->  172.19.26.33(4902) 
Mar 17 00:04:14   %ASA-5-106100: access-list   permitted tcp  10.252.0.165(50826) ->  172.19.26.33(4902) 
Mar 17 00:04:16   %ASA-5-106100: access-list   permitted tcp  10.252.0.166(54580) ->  172.19.26.33(4902) 
Mar 17 00:04:28   %ASA-5-106100: access-list   permitted tcp  10.252.0.222(38088) ->  172.19.26.33(4902) 
Mar 17 00:04:45   %ASA-5-106100: access-list   permitted tcp  10.252.0.221(17881) ->  172.19.26.33(4902) 

我想保持(4902)完好无损,但也希望用他们的数字取出第一个括号。

这是正确的吗?

awk '{sub('()'..... dst'()'," dst")}1'

3 个答案:

答案 0 :(得分:4)

您可以使用sed

sed 's/([0-9]*)//' logfile

答案 1 :(得分:1)

在awk中取出带有数字的第一个括号

$ awk '{sub(/\([^)]*\)/,"")}1' foo
Mar 17 00:03:13   %ASA-5-106100: access-list   permitted tcp  10.252.0.165 ->  172.19.26.33(4902) 
Mar 17 00:03:16   %ASA-5-106100: access-list   permitted tcp  10.252.0.166 ->  172.19.26.33(4902) 

答案 2 :(得分:1)

@Bhavik:试试:

awk '{sub(/\([0-9]+\)/,"");print}'   Input_file

它删除了第一次出现(所有数字然后)。然后它从Input_file打印行。