我有这样的档案:
1.0.0.2
32
255.0.0.6
0
32.0.191.171
64
32
128.1.1.1
128
64
128
97.4.100.3
32
它应该是这样的(文件包含IP和数字)。每个IP后面都应该有一个数字,而不是更多。如果一个IP后面跟着两个或两个以上的数字,则应删除IP和数字):
1.0.0.2
32
255.0.0.6
0
97.4.100.3
32
我尝试了一些使用awk (get line,prev)的代码,但不幸的是无法完成。我不知道这是否有帮助,但我认为这段代码应该识别IP:
awk --re-interval '{
if ($0 ~ /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/)
{
}
}'
发布于 2015-07-10 12:50:53
使用sed:
sed '/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/! { H; $!d; }; x; //!d; /\n.*\n/d' filename
这会将最后找到的IP地址和后面的行保存在hold缓冲区中,直到找到下一个IP地址或文件结束为止,然后检查其中是否有两行以上,并相应地打印或丢弃该块。
LIne按行排列:
# If there is no IP address in the current line (note the ! after the condition)
/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/! {
# append the line to the hold buffer.
H
# and unless it is the last one, continue with the next line, not printing
# anything.
$!d
}
# We only get here if there was an IP address or this is the last line. Swap in
# the remembered things from the hold buffer (placing the IP address in the hold
# buffer at the same time)
x
# If there's no IP address in the stuff we swapped in (// reattempts the last
# regex, which is the IP regex, and that there isn't one happens the very first
# time we get here), discard it.
//!d
# If there are two newlines in it, discard it also.
/\n.*\n/d
# Otherwise we drop off here and it is printed.
发布于 2015-07-10 12:53:52
以下代码将行与.
匹配,并开始在变量acc
中积累行。其他行通过中间的换行符连接到变量上。在最后,或者当一行有一个.
时,如果acc
变量正好有一个换行符(即分成两个部分),则打印它。把你的数据传送到里面。
awk '/\./{ if(split(acc,x,"\n")==2)print acc; acc = $1; next }
{ acc = acc "\n" $0 }
END{ if(split(acc,x,"\n")==2)print acc }'
发布于 2015-07-10 12:58:32
perl方式:
$/=undef;
my $str = <DATA>;
$str =~ s/\d+(?:\.\d+){3}\R(?:\d+\R){2,}//g;
say $str;
__DATA__
1.0.0.2
32
255.0.0.6
0
32.0.191.171
64
32
128.1.1.1
128
64
128
97.4.100.3
32
输出:
1.0.0.2
32
255.0.0.6
0
97.4.100.3
32
https://stackoverflow.com/questions/31340699
复制相似问题