读取文本文件;复制非缩进文本行;用所述文本替换缩进行的开头

读取文本文件;复制非缩进文本行;用所述文本替换缩进行的开头

我有一个文本文件,我想在其中操作文本。该文件的示例部分如下。我想读取第一个非缩进行,并将该文本插入到下面所有缩进行的开头。但是,正如您从示例部分中看到的那样(文件有 12k 行长,每个部分的长度各不相同),最终缩进的行停止,有一个空行,然后是一个我想要的新的非缩进字符串插入到下一组缩进行的开头。当前,文本之间的缩进和空白都是制表符,但是可以轻松地在整个文档中用“|”之类的内容替换。

所以......修改前的文件示例:

SOME_LINE_OF_TEXT
    Interface   Status  Protocol    Description
    Vl1 up  up
    Gi0/1   admin down  down
    Gi0/2   admin down  down
    Gi0/3   admin down  down
    Gi0/4   admin down  down
    Gi0/5   admin down  down
    Gi0/6   admin down  down
    Gi0/7   admin down  down
    Gi0/8   admin down  down
    Gi0/9   admin down  down
    Gi0/10  admin down  down
    Gi0/11  admin down  down
    Gi0/12  admin down  down
    Gi0/13  admin down  down
    Gi0/14  admin down  down
    Gi0/15  admin down  down
    Gi0/16  admin down  down
    Gi0/17  admin down  down
    Gi0/18  admin down  down
    Gi0/19  admin down  down
    Gi0/20  admin down  down
    Gi0/21  admin down  down
    Gi0/22  admin down  down
    Gi0/23  admin down  down
    Gi0/24  admin down  down
    Gi0/25  admin down  down
    Gi0/26  admin down  down
    Gi0/27  admin down  down
    Gi0/28  up  up  Node Name

SOME_OTHER_DIFFERENT_LINE_OF_TEXT
    Interface   Status  Protocol    Description
    Vl1 up  up
    Gi0/1   down    down    Noce Name
    Gi0/2   admin down  down
    Gi0/3   admin down  down
    Gi0/4   admin down  down
    Gi0/5   admin down  down
    Gi0/6   admin down  down
    Gi0/7   admin down  down
    Gi0/8   admin down  down
    Gi0/9   admin down  down
    Gi0/10  admin down  down
    Gi0/11  admin down  down
    Gi0/12  admin down  down
    Gi0/13  admin down  down
    Gi0/14  admin down  down
    Gi0/15  admin down  down
    Gi0/16  admin down  down
    Gi0/17  admin down  down
    Gi0/18  admin down  down
    Gi0/19  admin down  down
    Gi0/20  admin down  down
    Gi0/21  admin down  down
    Gi0/22  admin down  down
    Gi0/23  up  up  Node Name
    Gi0/24  up  up  Node Name
    Gi0/25  admin down  down
    Gi0/26  admin down  down
    Gi0/27  admin down  down
    Gi0/28  up  up  Node Name

期望的最终结果:

SOME_LINE_OF_TEXT
SOME_LINE_OF_TEXT   Interface   Status  Protocol    Description
SOME_LINE_OF_TEXT   Vl1 up  up
SOME_LINE_OF_TEXT   Gi0/1   admin down  down
SOME_LINE_OF_TEXT   Gi0/2   admin down  down
SOME_LINE_OF_TEXT   Gi0/3   admin down  down
SOME_LINE_OF_TEXT   Gi0/4   admin down  down
SOME_LINE_OF_TEXT   Gi0/5   admin down  down
SOME_LINE_OF_TEXT   Gi0/6   admin down  down
SOME_LINE_OF_TEXT   Gi0/7   admin down  down
SOME_LINE_OF_TEXT   Gi0/8   admin down  down
SOME_LINE_OF_TEXT   Gi0/9   admin down  down
SOME_LINE_OF_TEXT   Gi0/10  admin down  down
SOME_LINE_OF_TEXT   Gi0/11  admin down  down
SOME_LINE_OF_TEXT   Gi0/12  admin down  down
SOME_LINE_OF_TEXT   Gi0/13  admin down  down
SOME_LINE_OF_TEXT   Gi0/14  admin down  down
SOME_LINE_OF_TEXT   Gi0/15  admin down  down
SOME_LINE_OF_TEXT   Gi0/16  admin down  down
SOME_LINE_OF_TEXT   Gi0/17  admin down  down
SOME_LINE_OF_TEXT   Gi0/18  admin down  down
SOME_LINE_OF_TEXT   Gi0/19  admin down  down
SOME_LINE_OF_TEXT   Gi0/20  admin down  down
SOME_LINE_OF_TEXT   Gi0/21  admin down  down
SOME_LINE_OF_TEXT   Gi0/22  admin down  down
SOME_LINE_OF_TEXT   Gi0/23  admin down  down
SOME_LINE_OF_TEXT   Gi0/24  admin down  down
SOME_LINE_OF_TEXT   Gi0/25  admin down  down
SOME_LINE_OF_TEXT   Gi0/26  admin down  down
SOME_LINE_OF_TEXT   Gi0/27  admin down  down
SOME_LINE_OF_TEXT   Gi0/28  up  up  Node Name

SOME_OTHER_DIFFERENT_LINE_OF_TEXT
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Interface   Status  Protocol    Description
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Vl1 up  up
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/1   down    down    Noce Name
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/2   admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/3   admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/4   admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/5   admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/6   admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/7   admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/8   admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/9   admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/10  admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/11  admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/12  admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/13  admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/14  admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/15  admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/16  admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/17  admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/18  admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/19  admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/20  admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/21  admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/22  admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/23  up  up  Node Name
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/24  up  up  Node Name
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/25  admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/26  admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/27  admin down  down
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/28  up  up  Node Name

我目前正在手动查找并替换 Notepad++,但是,由于有 12k 行,这将花费比我愿意花费的时间更多的时间。

答案1

可能的解决方案:

awk '{ if (/^[A-Z]/) { p=$0; print } else if (/^$/) { print "" } else { print p$0 } }' /path/to/file

如果一行以非空白值开头,则保存并打印它,否则在该行前面添加最后存储的值。

相关内容