Here's Python 3 variant of @Ashwini Chaudhary's answer, to remove all lines that contain a regex pattern from a give filename:
#!/usr/bin/env python3 """Usage: remove-pattern <pattern> <file>""" import fileinput import re import sys def main(): pattern, filename = sys.argv[1:] # get pattern, filename from command-line matched = re.compile(pattern).search with fileinput.FileInput(filename, inplace=1, backup='.bak') as file: for line in file: if not matched(line): # save lines that do not match print(line, end='') # this goes to filename due to inplace=1 main()
It assumes locale.getpreferredencoding(False) == input_file_encoding otherwise it might break on non-ascii characters.
To make it work regardless what current locale is or for input files that have a different encoding:
#!/usr/bin/env python3 import os import re import sys from tempfile import NamedTemporaryFile def main(): encoding = 'utf-8' pattern, filename = sys.argv[1:] matched = re.compile(pattern).search with open(filename, encoding=encoding) as input_file: with NamedTemporaryFile(mode='w', encoding=encoding, dir=os.path.dirname(filename), delete=False) as outfile: for line in input_file: if not matched(line): print(line, end='', file=outfile) os.replace(outfile.name, input_file.name) main()
inputfile.readlines()inputfileis confusing at best.re.subis about replacing the matching content of a string. Not testing if a string match.