Python/bash 去除/输出 包含关键字的行

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

 

猜你喜欢

转载自blog.csdn.net/jack_ricky/article/details/79480108