Bash + debian。
匹配信息末尾的端口号。
s="2017-04-17 08:16:14 INFO connecting lh3.googleusercontent.com:443 from 111.111.111.111:26215"
echo $s | sed 's/\(.*\):\(\d*\)/\2/'
26215
让它与sed中的\ d +或\ d +匹配。
echo $s | sed 's/\(.*\):\(\d\+\)/\2/'
echo $s | sed 's/\(.*\):\(\d+\)/\2/'
所有这些都将整个字符串作为输出。
2017-04-17 08:16:14 INFO connecting lh3.googleusercontent.com:443 from 111.111.111.111:26215
最后他们都不能匹配端口号,为什么?
答案 0 :(得分:3)
可以使用更简单的sed
模式:
$ echo "$s" | sed -nE 's/.*:([^:])/\1/p'
26215
正如评论中所述,常规sed
没有perl
元字符。您需要使用[[:digit:]]
说明:
sed -nE 's/.*:([^:])/\1/p'
^ only print if there is a match
^ use ERE and you don't need to escape the parens
^ capture up to the rightmost :
^ ^ -E means you don't need to escape parens
^ all characters except :
^ print if there is a match
或者,如果您想要更具体,您只需要数字:
$ echo "$s" | sed -nE 's/.*:([[:digit:]]+$)/\1/p'
26215
请注意+
以确保至少有一位数字$
仅在该行的末尾匹配。
总结了不同的正则表达式HERE。使用-E
sed使用ERE与egrep相同。
答案 1 :(得分:2)
rows : for row in 1 to n-1 generate -- shouldn't this only go to (n/2) ?
columns : for col in 0 to n-1 generate
element: case row generate
-- when 0 no longer occurs with modified range
when 1 =>
first: entity work.fa
port map (
A => Ain(row)(col),
B => Bin(row)(col),
sum => sum(row+1)(col),
cin => cin,
cout => cout(row)(col+1)
);
when n/2 =>
last: entity work.fa
port map (
A => Ain(row)(col),
B => Bin(row)(col-offset),
sum => sum(row)(col-offset),
cin => cin,
cout => cout(row)(col)
);
when others =>
middle: entity work.fa
port map (
A => mandq(row)(col-offset),
B => sout(row)(col-offset),
sum => sout(row)(col-offset),
cin => cout(row)(col),
cout => cout(row)(col+1)
);
end generate;
end generate;
end generate;
是BRE或ERE语法中不存在的PCRE扩展(由标准UNIX工具使用)。
在这种特殊情况下,根本不需要使用任何未构建为bash的工具:
\d
这是parameter expansion;在处理少量数据时,这种内置功能比运行外部工具更有效。
shell中还内置了本机ERE支持,如下所示:
s="2017-04-17 08:16:14 INFO connecting lh3.googleusercontent.com:443 from 111.111.111.111:26215"
echo "Port is ${s##*:}"
BashFAQ #100也详细介绍了bash字符串操作。
答案 2 :(得分:1)
您需要的是这个
TEXT(timevalue,"mm-dd-yyyy hh:mm")
了解您的Shell字符串运算符。
答案 3 :(得分:0)
s="2017-04-17 08:16:14 INFO connecting lh3.googleusercontent.com:443 from 111.111.111.111:26215"
1.grep
echo $s |grep -Po '\d+$'
2.ack
echo $s |ack -o '\d+$'
3.sed
echo $s |sed 's/.*\://'
4.awk
echo $s |awk -F: '{print $NF}'
答案 4 :(得分:0)
OP的自我回答从问题转移到社区维基回答,按consensus on meta:
没有表达 \ d 代表sed中的数字。
简单地使用awk来获取:
echo $s |awk -F: '{print $NF}'
26215