Mastering sed: 30+ Powerful Text Manipulation Tricks for Linux
This comprehensive guide walks you through the essential and advanced features of the Linux sed command, showing how to perform bulk text replacements, deletions, insertions, line extraction, and powerful integrations with find, grep, awk, xargs, and tee, enabling efficient automation of log analysis, configuration updates, and data processing tasks.
What is sed?
sed (Stream Editor) is a command‑line utility that edits text streams without opening files, allowing you to substitute, delete, insert, or print text directly from the terminal.
Basic Operations
Replace text : sed 's/old/new/' file.txt Global replace (all matches): sed 's/old/new/g' file.txt In‑place edit (modify the file): sed -i 's/old/new/g' file.txt Use -i.bak to keep a backup.
Delete a line by number : sed '2d' file.txt Delete the last line : sed '$d' file.txt Delete lines matching a pattern : sed '/pattern/d' file.txt Print specific lines (e.g., line 2): sed -n '2p' file.txt Print a range of lines (2‑4): sed -n '2,4p' file.txt Insert text before a line : sed '2i\Inserted line' file.txt Append text after a line : sed '3a\Appended line' file.txt Change a specific line :
sed '3c\New content' file.txtAdvanced Patterns
Remove leading and trailing whitespace: sed 's/^[ \t]*//;s/[ \t]*$//' file.txt Strip HTML tags: sed 's/<[^>]*>//g' file.html Delete comment lines starting with # or //:
sed '/^#/d' config.txt
sed '/^\/\//d' code.cppUsing Multiple Commands
The -e option lets you chain several operations in one call.
sed -e 's/Alice/Jane/' -e 's/Bob/John/' file.txtYou can also combine deletion and substitution:
sed -e '/^#/d' -e 's/error/ERROR/g' file.txtCombining sed with Other Tools
Batch edit many files with find:
find /path -type f -name "*.txt" -exec sed -i 's/hello/hi/g' {} +Modify only lines that contain a pattern using grep: grep "error" file.txt | sed 's/failed/FAILED/g' Target a specific column in CSV files with awk and sed:
awk -F, '{ $2=gensub(/low/,"LOW","g",$2); print }' OFS=, file.csvProcess many files efficiently with xargs:
find /var/log -type f -name "*.log" | xargs sed -i 's/DEBUG/INFO/g'Preview changes before writing using tee:
sed 's/8080/9090/g' config.conf | tee new_config.confShow a diff of the transformation before applying it:
diff <(cat file.txt) <(sed 's/error/ERROR/g' file.txt)Conclusion
sed is a versatile stream editor for searching, replacing, deleting, and inserting text. By mastering its basic commands and learning to combine it with tools like find, grep, awk, xargs, and tee, you can automate repetitive text‑processing tasks, clean logs, update configuration files, and handle structured data efficiently.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
