Unix: Remove lines from a file based on regular expression
Published:
Just a note to self. Useful for trimming away useless information from a log file for example.
# Output result
$ sed "/pattern/d" file.log
# Overwrite file
$ sed "/pattern/d" file.log > file.log
# Inplace deletion (requires GNU sed)
$ sed -i "/pattern/d" file.log
In the pattern, things like capturing groups and alternations needs to be escaped with a slash, \
. If you have RegexBuddy you can use the GNU BRE flavor to help you construct the pattern.
- Source
- StackOverflow