DEV Community

Cover image for Mastering `sed` Commands and Flags: A Guide to Stream Editing in Linux πŸ–₯️
Eshan Roy (eshanized)
Eshan Roy (eshanized)

Posted on

Mastering `sed` Commands and Flags: A Guide to Stream Editing in Linux πŸ–₯️

sed (Stream Editor) is one of the most powerful text processing tools in Linux. Whether you're performing basic text replacement, deleting lines, or transforming entire files, sed provides a variety of commands and flags that can be used to manipulate text streams effectively.

In this post, we’ll dive into sed commands and their flags, explore common use cases, and look at examples to make you a sed master! πŸ› οΈ

🎯 Common sed Commands & Flags Explained

1. Substitution Command (s)

The substitution command is by far the most commonly used in sed. It searches for a pattern and replaces it with a replacement string.

Syntax:

sed 's/pattern/replacement/flags' file
Enter fullscreen mode Exit fullscreen mode

Example: Basic Substitution

sed 's/foo/bar/' file.txt
Enter fullscreen mode Exit fullscreen mode
  • This replaces the first occurrence of foo with bar in each line of file.txt.

Example: Global Substitution

sed 's/foo/bar/g' file.txt
Enter fullscreen mode Exit fullscreen mode
  • Replaces all occurrences of foo with bar in each line.

Example: Case-Insensitive Substitution

sed 's/foo/bar/i' file.txt
Enter fullscreen mode Exit fullscreen mode
  • Replaces foo with bar regardless of case (i.e., Foo, fOO, etc., are also replaced).

Example: N-th Occurrence Substitution

sed 's/foo/bar/2' file.txt
Enter fullscreen mode Exit fullscreen mode
  • Replaces the second occurrence of foo with bar in each line.

2. Delete Command (d)

The delete command removes lines that match a given pattern or condition.

Syntax:

sed '/pattern/d' file
Enter fullscreen mode Exit fullscreen mode

Example: Delete Lines with a Specific Word

sed '/error/d' file.txt
Enter fullscreen mode Exit fullscreen mode
  • This deletes all lines containing the word error from file.txt.

Example: Delete Blank Lines

sed '/^$/d' file.txt
Enter fullscreen mode Exit fullscreen mode
  • Deletes blank lines (lines with no content).

Example: Delete Lines Starting with a Number

sed '/^[0-9]/d' file.txt
Enter fullscreen mode Exit fullscreen mode
  • Deletes all lines starting with a digit.

3. Print Command (p)

The print command allows you to print lines matching a given pattern. It is often used with the -n flag to suppress the default behavior of printing all lines.

Syntax:

sed -n '/pattern/p' file
Enter fullscreen mode Exit fullscreen mode

Example: Print Lines Containing a Specific Word

sed -n '/error/p' file.txt
Enter fullscreen mode Exit fullscreen mode
  • This prints only the lines containing the word error.

Example: Print Matching Lines with the Next Line

sed -n '/error/{N; p}' file.txt
Enter fullscreen mode Exit fullscreen mode
  • Prints the matching lines along with the next line (this uses the N command to append the next line to the pattern space).

4. Insert Command (i)

The insert command adds a line before the specified line number or pattern.

Syntax:

sed 'line_number i\text_to_insert' file
Enter fullscreen mode Exit fullscreen mode

Example: Insert Text Before a Line

sed '2i\This is inserted before line 2' file.txt
Enter fullscreen mode Exit fullscreen mode
  • Inserts text before line 2.

Example: Insert Text Before a Matching Pattern

sed '/error/i\This is an error message' file.txt
Enter fullscreen mode Exit fullscreen mode
  • Inserts "This is an error message" before every line containing error.

5. Append Command (a)

The append command adds a new line after the specified line number or pattern.

Syntax:

sed 'line_number a\text_to_append' file
Enter fullscreen mode Exit fullscreen mode

Example: Append Text After a Line

sed '2a\This is appended after line 2' file.txt
Enter fullscreen mode Exit fullscreen mode
  • Appends text after line 2.

Example: Append Text After a Matching Pattern

sed '/error/a\This is an error resolution step' file.txt
Enter fullscreen mode Exit fullscreen mode
  • Appends "This is an error resolution step" after every line containing error.

6. Change Command (c)

The change command replaces the entire content of a line with new text.

Syntax:

sed 'line_number c\new_text' file
Enter fullscreen mode Exit fullscreen mode

Example: Change Content of a Line

sed '2c\This is the new content for line 2' file.txt
Enter fullscreen mode Exit fullscreen mode
  • Replaces the content of line 2 with This is the new content for line 2.

Example: Replace Lines Matching a Pattern

sed '/error/c\Critical error occurred' file.txt
Enter fullscreen mode Exit fullscreen mode
  • Replaces all lines containing error with Critical error occurred.

7. Address Ranges

You can apply sed commands to a range of lines using either line numbers or patterns.

Syntax for Line Numbers:

sed 'start_line,end_line command' file
Enter fullscreen mode Exit fullscreen mode

Example: Delete Lines 2 to 4

sed '2,4d' file.txt
Enter fullscreen mode Exit fullscreen mode
  • Deletes lines 2 to 4 from file.txt.

Syntax for Pattern-based Range:

sed '/start_pattern/,/end_pattern/d' file
Enter fullscreen mode Exit fullscreen mode

Example: Delete Lines Between Patterns

sed '/start/,/end/d' file.txt
Enter fullscreen mode Exit fullscreen mode
  • Deletes lines between (and including) the first occurrence of start and end.

8. Multiple Commands with -e Flag

The -e flag allows you to execute multiple commands sequentially.

Syntax:

sed -e 'command1' -e 'command2' file
Enter fullscreen mode Exit fullscreen mode

Example:

sed -e 's/foo/bar/' -e 's/baz/qux/' file.txt
Enter fullscreen mode Exit fullscreen mode
  • First, it replaces foo with bar, then it replaces baz with qux.

9. Suppress Output with -n Flag

By default, sed prints every line. The -n flag suppresses this, allowing you to print only the lines you need.

Syntax:

sed -n 'pattern p' file
Enter fullscreen mode Exit fullscreen mode

Example:

sed -n '/pattern/p' file.txt
Enter fullscreen mode Exit fullscreen mode
  • Prints only the lines containing pattern.

10. In-Place Editing with -i Flag

The -i flag enables in-place editing, meaning the file is modified directly without needing redirection.

Syntax:

sed -i 's/foo/bar/g' file
Enter fullscreen mode Exit fullscreen mode

Example: In-Place Substitution

sed -i 's/foo/bar/g' file.txt
Enter fullscreen mode Exit fullscreen mode
  • Replaces all occurrences of foo with bar and saves the changes to file.txt.

Example: In-Place Editing with Backup

sed -i.bak 's/foo/bar/g' file.txt
Enter fullscreen mode Exit fullscreen mode
  • Creates a backup file file.txt.bak before making the substitution.

11. Extended Regular Expressions with -E Flag

To enable extended regular expressions (ERE) in sed, use the -E flag. ERE allows more powerful patterns, such as +, ?, and |.

Syntax:

sed -E 's/[0-9]+/number/' file
Enter fullscreen mode Exit fullscreen mode

Example:

sed -E 's/\b[0-9]{3}\b/NUMBER/' file.txt
Enter fullscreen mode Exit fullscreen mode
  • Replaces any 3-digit number with NUMBER.

12. Escaping Special Characters

sed treats certain characters (like /, &, and \) as special. To use them literally, you must escape them with a backslash (\).

Example with Special Characters:

sed 's/foo/bar\&baz/' file.txt
Enter fullscreen mode Exit fullscreen mode
  • Replaces foo with bar&baz (escaping &).

πŸš€ Conclusion

sed is an incredibly versatile tool for text manipulation in Linux. From simple substitutions to complex pattern matching and file modifications, sed can handle almost any task related to text processing. By mastering the commands and flags covered here, you can greatly enhance your text manipulation skills.

Don’t hesitate to experiment with these commands to solve your text processing needs, and let me know in the comments if you have any questions or need further clarification! πŸ‘¨β€πŸ’»πŸ‘©β€πŸ’»

Happy editing with sed! βœ‚οΈ

Top comments (0)