我想从中删除一些文字 file1.txt。
我把文本放在文件中 tmp 并做:
grep -f tmp file.txt
但它只给我带来了不同。
问题是如何消除差异 file.txt。
我想从中删除一些文字 file1.txt。
我把文本放在文件中 tmp 并做:
grep -f tmp file.txt
但它只给我带来了不同。
问题是如何消除差异 file.txt。
Answers:
干 grep -f tmp file.txt 将显示包含该工作的所有行 text (假设 tmp 只是工作 text )。如果要显示所有不包含单词文本的行,则需要使用 -v 反转匹配的选项:
$ grep -v 'text' file.txt
如果您打印文件中的所有行,但只删除所有出现的行 text 然后:
$ sed 's/text//g'
如果你想删除你的行 file.txt 其中包含行 text 是种子然后你可以做类似的事情:
sed '/text/d' file.txt
要么
sed -n '/text/!p' file.txt
你想做的是
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
我所做的是:
sed '/text_to_delete/d' filename | sponge filename
这将更改源文件。
sed -i '/text_to_delete/d' filename
sed带有模式的命令脚本tmp。