
에서 일부 텍스트를 제거하고 싶습니다 file1.txt
.
파일에 텍스트를 넣고 tmp
다음을 수행합니다.
grep -f tmp file.txt
그러나 그것은 나에게 차이점만을 제공합니다.
문제는 file.txt
.
답변1
Do를 실행하면 grep -f tmp file.txt
해당 단어가 포함된 모든 줄이 표시됩니다 text
( tmp
단어만 포함한다고 가정 text
). 텍스트라는 단어가 포함되지 않은 모든 줄을 표시하려면 -v
일치 항목을 반전시키는 옵션을 사용해야 합니다.
$ grep -v 'text' file.txt
파일의 모든 행을 인쇄하고 text
then 항목을 모두 제거하는 경우:
$ sed 's/text//g'
답변2
file.txt
시드 가 있는 줄이 포함된 줄을 제거하려면 text
다음과 같이 할 수 있습니다.
sed '/text/d' file.txt
또는
sed -n '/text/!p' file.txt
답변3
당신이하고 싶은 일은
grep -Fvf tmp file.txt
에서 man grep
:
-f FILE, --file=FILE
Obtain patterns from FILE, one per line. The
empty file contains zero patterns, and
therefore matches nothing. (-f is specified
by POSIX.)
-F, --fixed-strings
Interpret PATTERN as a list of fixed strings,
separated by newlines, any of which is to be
matched. (-F is specified by POSIX.)
-v, --invert-match
Invert the sense of matching, to select non-
matching lines. (-v is specified by POSIX.)
따라서 파일에서 검색할 패턴 목록을 읽도록 -f
지시합니다 . 필요하므로 이러한 패턴을 정규식으로 해석하지 않습니다. 따라서 와 같은 문자열이 주어지면 는 "모든 문자와 일치"가 아닌 리터럴로 간주됩니다 . 마지막으로 는 일치 항목을 반전시켜 의 패턴과 일치하지 않는 줄만 인쇄합니다 . 예를 들어:grep
-F
grep
foo.bar
.
.
-v
grep
tmp
$ cat pats
aa
bb
cc
$ cat file.txt
This line has aa
This one contains bb
This one contains none of the patterns
This one contains cc
$ grep -Fvf pats file.txt
This one contains none of the patterns
답변4
내가 하는 일은:
sed '/text_to_delete/d' filename | sponge filename
이렇게 하면 소스 파일이 변경됩니다.