![파일의 특정 문자열 뒤에 파일 내용을 삽입하는 방법은 무엇입니까? [복사]](https://linux55.com/image/134193/%ED%8C%8C%EC%9D%BC%EC%9D%98%20%ED%8A%B9%EC%A0%95%20%EB%AC%B8%EC%9E%90%EC%97%B4%20%EB%92%A4%EC%97%90%20%ED%8C%8C%EC%9D%BC%20%EB%82%B4%EC%9A%A9%EC%9D%84%20%EC%82%BD%EC%9E%85%ED%95%98%EB%8A%94%20%EB%B0%A9%EB%B2%95%EC%9D%80%20%EB%AC%B4%EC%97%87%EC%9E%85%EB%8B%88%EA%B9%8C%3F%20%5B%EB%B3%B5%EC%82%AC%5D.png)
예를 들어 다음 파일이 있습니다.
cat exam.txt
I am expert linux man
what we can do for out country
I love redhat machine
"propertie"
centos less then redhat
fedore what
my name is moon yea
속성 행 뒤에 파일의 내용을 file.txt로 추가하고 싶습니다.
cat file.txt
324325
5326436
3245235
646346
545
643
6436
63525
664
46454
그래서 나는 다음을 시도합니다.
a=` cat file `
sed -i '/propertie/a `echo "$a"` ' exam.txt
하지만 작동하지 않습니다
특정 문자열 뒤에 파일 내용을 추가하기 위한 sed/awk/perl 라이너에 대한 제안 사항이 있습니까?
예상 출력
I am expert linux man
what we can do for out country
I love redhat machine
"propertie"
324325
5326436
3245235
646346
545
643
6436
63525
664
46454
centos less then redhat
fedore what
my name is moon yea
답변1
Unix 쉘 스크립트의 변수에 파일의 전체 내용을 저장하고 싶지는 않을 것입니다. 이 작업을 수행하는 경우 다른 해결 방법이 있는지 자문해 보세요. 직접 찾을 수 없다면 여기로 오셔서 살펴보세요 :-)
$ sed '/propertie/r file.txt' exam.txt
I am expert linux man
what we can do for out country
I love redhat machine
"propertie"
324325
5326436
3245235
646346
545
643
6436
63525
664
46454
centos less then redhat
fedore what
my name is moon yea
r
in의 ("read") 명령은 sed
파일 이름을 인수로 사용하고 파일 내용을 현재 스트림에 삽입합니다.
file.txt
추가된 내용을 들여쓰기해야 하는 경우 다음을 실행하기 전에 내용을 들여쓰기해야 합니다 sed
.
$ sed 's/^/ /' file.txt >file-ind.txt
$ sed '/propertie/r file-ind.txt' exam.txt
I am expert linux man
what we can do for out country
I love redhat machine
"propertie"
324325
5326436
3245235
646346
545
643
6436
63525
664
46454
centos less then redhat
fedore what
my name is moon yea
있음 ed
( sed
삽입할 파일의 들여쓰기가 필요함). 또한 파일의 내부 편집을 수행하여 원본 파일을 수정된 콘텐츠로 바꿉니다.
ed -s exam.txt <<END_ED
/propertie/r !sed 's/^/ /' file.txt
wq
END_ED
r
in 명령은 명령 앞에 가 붙으면 ed
외부 명령의 출력을 읽을 수 있습니다 !
. 이를 사용하여 삽입하려는 데이터를 들여씁니다. 그 외에는 sed
분명한 이유로 위의 솔루션과 매우 유사합니다.
사용의 유일한 단점은 ed
일반적으로 매우 큰 파일에는 사용할 수 없다는 것입니다. sed
길이가 결정되지 않은 스트림을 편집하는 데 유용하며, ed
문서는 다른 편집기에서 열 수 있습니다(예: 크기가 메가바이트 또는 기가바이트가 아닌 파일).