
나는 이것을 알아 내려고 오랜 시간을 보냈습니다. 공교롭게도 더 이상 이 작업을 수행할 필요가 없어 다른 방법을 찾았습니다. 하지만 제 정신 상태를 위해 여전히 답을 알고 싶습니다.
sed
마크업을 일부 텍스트로 바꾸는 것은 매우 쉽습니다. 내가 이해하지 못하는 것은 sed
여러 줄, 탭 등과 같은 복잡한 서식을 사용하여 거대한 텍스트 블록을 삽입하는 방법입니다. 넌 어떻게 그걸 했니? sed
최고 인가 , 아니면 무엇인가?
답변1
ex
실제로 데이터 흐름이 없다면 이와 같은 작업 에 사용하겠습니다 . ex
여기 문서에 명령을 저장했습니다 .
ex $FILENAME << END_EX_COMMANDS
" Find the mark, assuming it's an entire line
/^MARKER$/
" Delete the marker line
d
" Add the complex, multi-line text
a
Complex, vividly formatted text here.
Even more "specially" formatted text.
.
" The '.' terminates the a-command. Write out changed file.
w!
q
END_EX_COMMANDS
사용은 ex
사용자에게 이점이 됩니다. 사용자는 이미 명령의 키 입력을 알고 있지만 맹목적으로 편집하는 느낌을 줍니다. 또한 ":" 모드에서 수행할 수 있는 모든 작업뿐만 아니라 여러 추가 또는 전역 교체를 수행할 수도 있습니다.vi
vim
vim
답변2
/tmp/insert.txt라는 파일의 표시에 텍스트를 삽입하려면 다음과 같이 이 작업을 수행할 수 있습니다.
sed '/MARKER/ r /tmp/insert.txt' < inputfile
위의 sed 명령은 "inputfile"을 읽고 MARKER를 찾습니다. 이를 찾으면 /tmp/insert.txt의 내용을 출력 스트림에 삽입합니다.
태그 자체를 제거하려면 다음을 시도하십시오.
sed '/MARKER/ {s/MARKER//; r /tmp/insert.txt
}' <inputfile
"}" 닫는 중괄호 앞에는 개행 문자가 와야 합니다.
첫 번째 명령과 마찬가지로 sed는 MARKER가 있는 줄에서 실행됩니다. MARKER를 공백으로 바꾼 다음 /tmp/insert.txt를 읽습니다.
답변3
나는 이 명령이 당신이 찾고 있는 것이라고 믿습니다:
r FILENAME
As a GNU extension, this command accepts two addresses.
Queue the contents of FILENAME to be read and inserted into the
output stream at the end of the current cycle, or when the next
input line is read. Note that if FILENAME cannot be read, it is
treated as if it were an empty file, without any error indication.
As a GNU `sed' extension, the special value `/dev/stdin' is
supported for the file name, which reads the contents of the
standard input.
답변4
태그 자체를 제거하려면 다음을 한 줄로 시도해 보십시오.
sed -e '/MARKER/ r /tmp/insert.txt' -e '/MARKER/d' < inputfile
또는
sed -e '/MARKER/ {r /tmp/insert.txt' -e 'd }' < inputfile