开发者社区> 问答> 正文

如何在特定列中的最后一个重复的字符串之后添加换行符

我正在尝试在列中最后一个重复的字符串之后添加换行符。这是我需要调整自己经常使用的命令输出的步骤之一,以使您的眼睛更加舒缓。

例如。我安排的输出当前为这种形式:

10.12   null   machineA
19.13   null   machineA
12.11   null   machineA
12.13   null   machineB
14.14   null   machineB
11.17   null   machineC
17.13   null   machineC
16.16   null   machineC

我想将输出查看为:

10.12   null   machineA
19.13   null   machineA
12.11   null   machineA

12.13   null   machineB
14.14   null   machineB

11.17   null   machineC
17.13   null   machineC
16.16   null   machineC

我认为sed或awk是我的朋友。朝着正确方向提供的任何帮助均深表感谢。

谢谢。

展开
收起
祖安文状元 2020-01-05 19:00:04 338 0
1 条回答
写回答
取消 提交回答
  • 假设文件按第三列排序,则可以使用prev和curr变量并打印结果

    awk ' { c=$3; if(c!=p && NR>1) { print "" } print ; p=c } ' file 用您给定的输入

    $ cat kalam.txt
    10.12   null   machineA
    19.13   null   machineA
    12.11   null   machineA
    12.13   null   machineB
    14.14   null   machineB
    11.17   null   machineC
    17.13   null   machineC
    16.16   null   machineC
    
    $ awk ' { c=$3; if(c!=p && NR>1) { print "" } print ; p=c } ' kalam.txt
    10.12   null   machineA
    19.13   null   machineA
    12.11   null   machineA
    
    12.13   null   machineB
    14.14   null   machineB
    
    11.17   null   machineC
    17.13   null   machineC
    16.16   null   machineC
    
    $
    
    2020-01-05 19:00:14
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载