首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >检查和更改下一行的值

检查和更改下一行的值
EN

Stack Overflow用户
提问于 2015-07-10 12:24:10
回答 5查看 76关注 0票数 2

我有这样的档案:

代码语言:javascript
运行
复制
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和数字):

代码语言:javascript
运行
复制
1.0.0.2
32
255.0.0.6
0
97.4.100.3
32

我尝试了一些使用awk (get line,prev)的代码,但不幸的是无法完成。我不知道这是否有帮助,但我认为这段代码应该识别IP:

代码语言:javascript
运行
复制
awk --re-interval '{
    if ($0 ~ /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/) 
    { 

    } 
    }' 
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2015-07-10 12:50:53

使用sed:

代码语言:javascript
运行
复制
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按行排列:

代码语言:javascript
运行
复制
# 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.
票数 -1
EN

Stack Overflow用户

发布于 2015-07-10 12:53:52

以下代码将行与.匹配,并开始在变量acc中积累行。其他行通过中间的换行符连接到变量上。在最后,或者当一行有一个.时,如果acc变量正好有一个换行符(即分成两个部分),则打印它。把你的数据传送到里面。

代码语言:javascript
运行
复制
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 }'
票数 1
EN

Stack Overflow用户

发布于 2015-07-10 12:58:32

perl方式:

代码语言:javascript
运行
复制
$/=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

输出:

代码语言:javascript
运行
复制
1.0.0.2
32
255.0.0.6
0
97.4.100.3
32
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31340699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档