DEV Community

Cover image for Sed It Right: Mastering the Stream Editor for Text Magic
Jimmy McBride
Jimmy McBride Subscriber

Posted on • Originally published at jimmymcbride.dev

Sed It Right: Mastering the Stream Editor for Text Magic

We all feel the pain of regex's weird syntax, but hey, we're not going to let that stop us from achieving greatness! Despite its quirks, regex is a powerhouse when used in combination with sed. Let’s harness its strength!

sed becomes a real powerhouse when you start using regex to match patterns more intelligently. For example, suppose you want to replace any sequence of digits with the word "[number]":

sed 's/[0-9]\+/[number]/g' text.txt
Enter fullscreen mode Exit fullscreen mode

This will replace any group of digits ([0-9]+) with "[number]" throughout the file. Regex allows you to create powerful and flexible searches, letting you target specific patterns of text, not just exact strings.


7. Inserting and Appending Text

Need to insert or append lines into a file? sed can do that too! To insert text before a specific line, use the i command:

sed '3i\This is inserted before line 3' file.txt
Enter fullscreen mode Exit fullscreen mode

This will insert the text "This is inserted before line 3" just before the 3rd line. To append text after a line, use a:

sed '3a\This is appended after line 3' file.txt
Enter fullscreen mode Exit fullscreen mode

8. Combining sed with Other Commands

Like all powerful shell commands, sed works beautifully when combined with other tools like cat, grep, or even find. Let’s say you want to find all text files in a directory and replace "apple" with "orange" in all of them:

find . -name "*.txt" -exec sed -i 's/apple/orange/g' {} \;
Enter fullscreen mode Exit fullscreen mode

This command finds every .txt file in the current directory and applies the sed substitution to replace "apple" with "orange" directly within each file.


9. Real-World Use Cases

sed is a lifesaver in real-world scenarios, like quickly making bulk text changes in configuration files, automating logs cleanup, or prepping data for further processing. For example:

  • Update URLs in HTML files: Replace old URLs with new ones in all .html files:
  sed -i 's/oldsite.com/newsite.com/g' *.html
Enter fullscreen mode Exit fullscreen mode
  • Clean up logs: Remove unnecessary log entries (e.g., removing "INFO" lines from log files):
  sed '/INFO/d' log.txt
Enter fullscreen mode Exit fullscreen mode

Wrapping Up

sed is a game-changer when it comes to stream editing and text manipulation. It’s fast, flexible, and powerful enough to handle everything from quick find-and-replace tasks to complex text transformations. Once you get comfortable with the basics, you’ll find sed to be an indispensable tool in your shell toolkit.

Next up in the Textual Healing series, we’re diving deep into awk, another heavy hitter for text processing that’ll help you analyze and manipulate data with ease.


Want to hang out with other Linux lovers and coding enthusiasts? Come join our community on Discord! We’re a group of friendly folks who love to code, share tips, and help each other grow. Click here to join the conversation!

Top comments (1)

Collapse
 
moopet profile image
Ben Sinclair

Note that this use of -i is for GNU sed, others have a different syntax which requires you to specify a backup pattern.