Python
import shutil
with open('/path/to/file', 'r') as f:
with open('/path/to/file.new', 'w') as g:
for line in f.readlines():
if '/local/server' not in line:
g.write(line)
shutil.move('/path/to/file.new', '/path/to/file')bash
grep -v '/local/server' filename类似的,还可以输出含有关键字的行:Python将not in改为in即可,bash依然使用grep命令:
grep "KW" target_file > res_file
本文介绍如何使用Python脚本从文件中过滤掉包含特定字符串的行,并提供了如何保留这些行的方法。通过Python标准库中的`shutil`模块完成文件替换,同时展示了等效的Bash命令。
5331

被折叠的 条评论
为什么被折叠?



