# Read Text File; Copy Non-Indented Text Line; Replace Beginning of Indented Lines with Said Text

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/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/23  up  up  Node Name
Gi0/24  up  up  Node Name
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/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/23  up  up  Node Name
SOME_OTHER_DIFFERENT_LINE_OF_TEXT   Gi0/24  up  up  Node Name

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
• @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