sed 파일 비교 [중복]

sed 파일 비교 [중복]

OSX에서 다음 명령은 패턴을 제거하고 전체 단어에 영향을 줍니다.

sed -e "$(sed 's:.*:s/&//g:' /path/to/wordsToRemove.txt)” /path/to/sourceFile.txt > outFile.txt

wordsToRemove.txt포함하다:

it
for

sourceFile.txt포함하다:

it was green forever for candy

outFile.txt포함하다:

was green ever candy

"for"라는 단어를 "forever"의 일부가 아닌 단어 자체로 일치시키고 싶었지만 "forever"라는 단어가 일치하여 "ever"로 변경되었습니다.

이 상황을 피할 수 있습니까?

답변1

당신은 일치 할 수 있습니다단어 경계sed에서는 정규식에 특수 태그를 사용하여 이를 수행합니다 \<.\>

예를 들어:

 $sed -e 's/\<for\>//g' < sourceFile.txt 
 it was green forever  candy

이 정규식은 "for"를 영원히의 일부가 아닌 전체 단어로만 일치시킵니다.

따라서 원래 oneliner를 다음으로 변경할 수 있습니다.

sed -e "$(sed 's:.*:s/\\<&\\>//g:' /path/to/wordsToRemove.txt)” /path/to/sourceFile.txt > outFile.txt

\<및 이스케이프 처리에 유의하세요 \>.

관련 정보