텍스트를 조작하려는 텍스트 파일이 있습니다. 파일의 예시 섹션은 다음과 같습니다. 들여쓰기되지 않은 첫 번째 줄을 읽고 아래에 있는 모든 들여쓰기 줄의 시작 부분에 해당 텍스트를 삽입하고 싶습니다. 그러나 예제 섹션에서 볼 수 있듯이(파일 길이는 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
행이 공백이 아닌 값으로 시작하면 저장되고 인쇄됩니다. 그렇지 않으면 마지막으로 저장된 값이 행 앞에 추가됩니다.