How to Remove Lines Starting with a Pattern from a File in Linux
Sometimes, when working with log files, backups, or system-generated data, you may want to delete lines that begin with a specific pattern. Luckily, Linux provides several quick and powerful ways to do this from the command line.
In this article, we’ll explore how to remove lines that start with a certain prefix or string using tools like sed and egrep. These methods are efficient and work well for automation or scripting.
Use Case Example
Let’s say you have a log file with a list of changed files, and you want to remove all lines that start with ./var/cache, which typically includes cached files that don’t need to be tracked in backups or version control.
🔧 Method 1: Using sed
The sed (stream editor) tool is very powerful for text processing. You can delete lines that match a certain pattern using the d command.
sed '/^\.\/var\/cache/d' /home/taras/website_backups/changed_files.log > /home/taras/website_backups/changed_files_cleaned.log
Explanation:
^\.\/var\/cache— Matches lines starting with./var/cached— Deletes the matched lines- The output is redirected to a new cleaned file
✅ Tip: Always test sed commands on a copy of your file to avoid accidental data loss.
🧰 Method 2: Using egrep (or grep -E)
Another simple and readable method is to use egrep or grep -E with the -v (invert match) option, which excludes matching lines.
egrep -v '^\.\/var\/cache' /home/taras/website_backups/changed_files.log > /home/taras/website_backups/changed_files_cleaned2.log
Or using grep -E:
grep -Ev '^\.\/var\/cache' /home/taras/website_backups/changed_files.log > /home/taras/website_backups/changed_files_cleaned2.log
🔍 How to Verify the Results
You can compare the original and the cleaned file using the diff command:
diff /home/taras/website_backups/changed_files.log /home/taras/website_backups/changed_files_cleaned.log
or
diff /home/taras/website_backups/changed_files.log /home/taras/website_backups/changed_files_cleaned2.log
This will show you exactly what lines were removed.
🧪 More Practical Examples
Remove comment lines starting with #:
sed '/^#/d' config.txt > config_cleaned.txt
Remove empty lines:
sed '/^$/d' notes.txt > notes_no_empty.txt
Remove all lines starting with "DEBUG" in a log:
grep -v '^DEBUG' app.log > app_cleaned.log
🛡️ Pro Tip: In-place Editing with sed
If you want to edit the file directly (be careful!):
sed -i '/^\.\/var\/cache/d' /home/taras/website_backups/changed_files.log
The -i flag tells sed to edit the file in place.
🧠 Conclusion
Whether you’re cleaning up logs, filtering backups, or preprocessing data for scripts, these simple command-line tricks with sed and grep can save you time and effort.
If you’re working with Linux regularly, mastering these tools is a must. They’re versatile, fast, and script-friendly.
📣 Share Your Use Case
Have you used similar commands in your daily work? Share your examples in the comments or drop me a message—I’d love to include more community-driven use cases in future posts!
Keywords for SEO:
Linux remove lines from file, delete lines starting with string Linux, grep exclude pattern, sed delete line, Linux command line tips, clean log files Linux
Would you like a thumbnail or social media caption to promote this post too?