テキストを操作したいテキストファイルがあります。ファイルの例のセクションは次のとおりです。インデントされていない最初の行を読み、下のすべてのインデント行の先頭にそのテキストを挿入したいと思います。ただし、例のセクションでわかるように(ファイルの長さは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 ++を使用して手動で検索と置換を実行していますが、行は12,000行なので、かかる時間よりも時間がかかります。
答え1
考えられる解決策:
awk '{ if (/^[A-Z]/) { p=$0; print } else if (/^$/) { print "" } else { print p$0 } }' /path/to/file
行が空白以外の値で始まる場合、保存され印刷されます。それ以外の場合は、最後に保存された値が行の前に追加されます。