1

I have a text file I want to manipulate the text in. An example portion of the file is below. I want to read the first non-indented line and insert that text at the beginning of all the following indented lines below. However, as you can see from the example portion (the file is 12k lines long with each section being of varying length), eventually the indented lines stop, there is a blank line, and then a new non-indented string that I would want inserted at the beginning of the next set of indented lines. The indent as well as white-space between text are all currently tabs, however, could easily be replaced throughout the document with something like "|".

So....file example before modification:

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

Desired End Result:

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

I am currently doing a manual find and replace thought Notepad++, however, with 12k lines, that is going to take more time than I would rather spend on this.

1

Possible solution:

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

If a line starts with a non-whitespace value, save and print it, otherwise prepend the line with last stored value.

  • phk.....THANK YOU. While your solution didn't get me there initially, it did push me in the right direction. I never thought about the conditional awk...that was brilliant and saved me SOOOO much time in this edit. Here is the solution I came up with based on your input that worked for me: awk '{ if (/^[A-Z]/) { prepend=$0 } else if (/^$/) { print "" } else { print prepend$0 } }' /path/to/file – usaf_comm Dec 27 '16 at 19:57
  • awk '{ if (/^[A-Z]/) { prepend=$0 } else if (/^$/) { print "" } else { print prepend$0 } }' /path/to/file – usaf_comm Dec 27 '16 at 19:58
  • @usaf_comm I just wanted to point out that your solution skips adding the initial "SOME_LINE_OF_TEXT" on a line by itself at the beginning of the file. Also, blank line won't be preserved if there are any spaces or tabs in them. This may not be important to you, but a slightly modified version of @phk solution appears to cover those issues. awk '{ if (/^\S/ || $2=="") { s=$0; print s } else { print s$0 } }' /path/to/file – Paul H. Dec 27 '16 at 20:29
  • 1
    @PaulH. Another modification which would solve this, would be to replace (/^$/) with (/^\s*$/) and unlike your solution it's independent of the FS. – phk Dec 28 '16 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.