Linux - Different ways to delete matching line

By xngo on June 30, 2019

# Shell(bash3.2+)
while read -r line
do
  [[ ! $s =~ pattern ]] && echo "$line"
done <file > o 
mv o file
 
# GNU grep
grep -v "pattern" file > temp && mv temp file
 
# sed (printing the inverse is faster than actual deletion). Need -n.
sed -n '/pattern/!p' file 

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.