🛠️ Command: sed (Stream Editor)
sed is a "non-interactive" text editor. Unlike opening a file in VS Code or Nano, sed takes text from a pipe or a file, performs transformations (like find-and-replace), and outputs the result.
1. The "Why"
For an Arch Linux power user and developer, sed is the ultimate tool for automated editing.
- Mass Find-and-Replace: Rename a variable across a whole Java source file or change a recurring term in your LaTeX e-book in one go.
- Config Automation: Quickly toggle settings in your Hyprland or Waybar config files without opening an editor.
- Log Cleaning: Strip out timestamps or unwanted prefixes from journalctl output to make it more readable.
- Scripted Edits: Use it in your "write tutorials for money" workflow to automatically format code snippets.
2. Basic Syntax
The most common use of sed is the substitution command:
sed 's/old_text/new_text/' filename
s: Stands for Substitute./: These are delimiters (you can actually use other characters like|or:if your text contains slashes).
3. Practical Examples for Your Workflow
A. Simple Find-and-Replace
To change the word "Flutter" to "Java" in a text file (only the first occurrence per line):
sed 's/Flutter/Java/' dev_notes.txt
B. Global Replacement (g)
By default, sed only changes the first match on each line. To change every match, add the g flag:
sed 's/Flutter/Java/g' dev_notes.txt
C. Delete Specific Lines
Delete the 3rd line of a file:
sed '3d' file.txt
Delete all lines that contain the word "DEBUG":
sed '/DEBUG/d' app.log
D. Editing "In-Place" (-i)
Standard sed just prints the result to your screen. To actually save the changes to the file, use the -i flag:
sed -i 's/user Codes/user/g' index.html
4. Advanced Tricks
1. Using Different Delimiters
If you are trying to change a URL (which has many /), the standard syntax gets messy. Use | instead:
sed 's|https://old-api.com|https://supabase-api.com|g' config.js
2. Addressing Specific Lines Change "Draft" to "Final" only on line 50 of your e-book:
sed '50s/Draft/Final/' blueprint.tex
3. Multiple Commands at Once
Use -e to string commands together:
sed -e 's/red/blue/g' -e 's/slow/fast/g' styles.css
5. Pro-Tips
- Dry Run First: Always run
sedwithout the-iflag first to make sure your pattern is correct before you permanently change your code. - Regex Power:
sedsupports regular expressions. For example,sed 's/^[0-9]*//' file.txtwill remove all numbers at the start of every line. - Arch Linux Context: You can use
sedto quickly change your wallpaper path in yourhyprland.confor update the color hex codes in your terminal config without manual typing.
6. Summary Reference
| Goal | Command |
|---|---|
| Find and Replace (First) | sed 's/old/new/' file |
| Find and Replace (All) | sed 's/old/new/g' file |
| Save changes to file | sed -i 's/old/new/g' file |
| Delete line 5 | sed '5d' file |
| Print only specific lines | sed -n '10,20p' file |
Next up: Should we look at grep (to find patterns across your entire project) or find (to locate files based on their size, date, or name)?