Introduction
If you work with text files on the command line, chances are you’ve already heard of the sed
command. It’s a powerful tool that allows you to perform complex text transformations in just a few keystrokes. Whether you’re looking to replace strings, delete lines, or extract specific patterns from a file, sed
has got you covered.
Requirements and Dependencies
Sed
is a command-line utility that is available on most Unix-like systems, including Linux and macOS. No additional installation is required – you can simply type sed
in your terminal and start using it.
Use-Cases
There are countless ways to use sed
, but here are a few common examples:
- Replacing text: Suppose you want to replace all occurrences of the word “lazy” with “hardworking” in a file. You can use
sed
like this:
sed 's/lazy/hardworking/g' input.txt > output.txt
- Deleting lines: Suppose you want to delete all lines in a file that contain the word “error”. You can use
sed
like this:
sed '/error/d' input.txt > output.txt
- Extracting patterns: Suppose you want to extract all email addresses from a file. You can use
sed
like this:
sed -n 's/.*\([[:alnum:]_.-]\+@[[:alnum:]_.-]\+\.[[:alpha:].]\+\).*/\1/p' input.txt > output.txt
Here’s an example of how you can use sed
to perform a text transformation on a website:
curl itvraag.nl | sed 's/question/answer/g'
This will fetch the contents of the website and replace all occurrences of the word “question” with “answer”.
Key Take-Aways
Sed
is a command-line tool for performing text transformations- It can be used to replace, delete, and extract patterns from text files
- It’s available on most Unix-like systems and doesn’t require any additional installation
Tips
- Use the
i
flag to edit a file in place, instead of writing to an output file - Use the
n
flag to suppress automatic printing of pattern space - Use the
r
flag to use extended regular expressions - Use the
\1
,\2
, etc. syntax to refer to capture groups in the replacement string - Use the
\l
,\u
, and\L
syntax to convert case in the replacement string
Additional Resources
- Use the
man
command to view the manual page forsed
:man sed
- Check out the GNU Sed documentation for more advanced usage examples
Challenge
Here’s a challenge to test your understanding of sed
. Try to solve the following task using sed
:
- Given a file containing lines in the following format: “name:age:city”, extract only the names and print them one per line.
Here’s an example of how your solution might look:
sed -n 's/^\([^:]\+\):.*$/\1/p' input.txt > output.txt
Remember to use the -n
flag to suppress automatic printing, and the ^
and $
anchors to match the beginning and end of each line, respectively.