NeuroAgent

sed Command: Delete Lines Containing String

Learn how to delete lines containing specific strings using sed command. Master pattern matching, case-insensitive options, and in-place editing with practical examples for text file processing.

Question

How to delete lines containing a specific string from a text file using sed?

NeuroAgent

To delete lines containing a specific string from a text file using sed, you can use the command sed '/pattern/d' filename.txt, where pattern is the string you want to match. This command will print all lines except those containing the specified pattern, and you can redirect the output to a new file or use the -i flag to edit the file in place. For case-insensitive matching, add the I flag: sed '/pattern/dI' filename.txt.

Contents

Basic Syntax and Usage

The fundamental syntax for deleting lines containing a specific string with sed is:

bash
sed '/pattern/d' input_file.txt

This command works by:

  • Matching lines that contain pattern
  • Using the d command to delete those matching lines
  • Printing all remaining lines to standard output

The pattern can be:

  • A simple string (e.g., /error/)
  • A regular expression (e.g., /^#/ to delete comment lines)
  • A fixed string using \ escaping

To save the output to a new file:

bash
sed '/pattern/d' input_file.txt > output_file.txt

To edit the file in place (make sure to backup first!):

bash
sed -i '/pattern/d' input_file.txt

Common Pattern Matching Techniques

Exact String Matching

bash
sed '/exact_string/d' filename.txt

Case-Insensitive Matching

bash
sed '/pattern/dI' filename.txt

Multiple Patterns

bash
# Delete lines containing any of the patterns
sed '/pattern1\|pattern2/d' filename.txt

Line Numbers and Ranges

bash
# Delete specific line number
sed '3d' filename.txt

# Delete line range
sed '5,10d' filename.txt

# Delete from line 10 to end
sed '10,$d' filename.txt

Regular Expressions

bash
# Delete lines starting with #
sed '/^#/d' filename.txt

# Delete empty lines
sed '/^$/d' filename.txt

# Delete lines containing numbers
sed '/[0-9]/d' filename.txt

Advanced sed Options

Multiple Pattern Files

bash
# Use patterns from a file
sed -f pattern_file.txt input_file.txt

Backup Before Editing

bash
# Create .bak backup before in-place editing
sed -i.bak '/pattern/d' filename.txt

Silent Operation

bash
# Suppress output (useful for scripts)
sed -n '/pattern/dp' filename.txt

Extended Regular Expressions

bash
# Use extended regex for more complex patterns
sed -E '/pattern[0-9]+/d' filename.txt

Practical Examples

Example 1: Log File Cleanup

bash
# Remove ERROR lines from log file
sed '/ERROR/d' application.log > clean_log.log

Example 2: Configuration File Cleanup

bash
# Remove commented lines (starting with # or ;)
sed -E '/^#|^;/d' config.ini

Example 3: Data Processing

bash
# Remove lines containing "temp" or "debug"
sed '/temp\|debug/d' data.csv

Example 4: HTML Processing

bash
# Remove empty HTML tags
sed -E '/<[^>]*><\/[^>]*>/d' webpage.html

Example 5: System Administration

bash
# Remove disabled services from systemd config
sed '/^#|^Enabled=no/d' services.conf

Troubleshooting Common Issues

Special Characters in Patterns

When patterns contain special regex characters, escape them:

bash
# Delete lines containing "file.txt"
sed '/file\.txt/d' filename.txt

Performance with Large Files

For very large files, use more efficient approaches:

bash
# Use grep to find line numbers first, then sed
grep -n "pattern" filename.txt | cut -d: -f1 | xargs -I {} sed -i "{}d" filename.txt

Preserving Original File Structure

Always backup before in-place editing:

bash
# Safe approach with backup
cp original.txt original.txt.bak
sed -i '/pattern/d' original.txt

Debugging Pattern Matching

Test your pattern before applying it:

bash
# Show which lines would be deleted
sed -n '/pattern/p' filename.txt

# Show line numbers of matches
grep -n "pattern" filename.txt

Sources

  1. GNU sed Manual - The d Command
  2. sed Tutorial - Deleting Lines
  3. Regular Expressions in sed
  4. Advanced sed Techniques

Conclusion

Deleting lines containing specific strings with sed is a powerful text processing technique that every Linux/Unix user should master. The basic sed '/pattern/d' command provides a simple yet flexible way to filter text files, while options like -i for in-place editing and -I for case-insensitive matching enhance its versatility. Remember to always backup important files before using in-place editing, and test your patterns on small samples first. For complex operations, consider combining sed with other tools like grep or awk for more sophisticated text processing workflows.