Fundamentals 6 min read

Common sed Commands for Text Manipulation with Regular Expressions

This article provides a concise collection of practical sed command examples, demonstrating how to print, delete, substitute, and transform lines and characters in a text file using regular expressions, including techniques for case conversion, line numbering, and in‑place editing.

Practical DevOps Architecture
Practical DevOps Architecture
Practical DevOps Architecture
Common sed Commands for Text Manipulation with Regular Expressions

This guide lists frequently used sed one‑liners for manipulating the contents of a file.

1. Print the first and third lines of 1.txt : sed -e '3p' -e '1p' -n 1.txt

2. Print from the first line to the last line (i.e., the whole file): sed -n '1,$'p 1.txt

3. Print lines 3 through 5: sed -n '3,5'p 1.txt

4. Delete all lines (from the first to the last): sed '1,$'d 1.txt

5. Delete the third and then the first line: sed -e '3d' -e '1d' 1.txt

6. Replace the letter r with R on the first line: sed '1s/r/R/g' 1.txt

7. Change lower‑case b to upper‑case B on lines 1‑2: sed '1,2s/b/B/g' 1.txt

8. General substitution syntax: s/pattern/replacement/g (global) or without g to replace only the first occurrence; any delimiter such as # can be used.

9. Replace every occurrence of nologin with NO : sed 's#nologin#NO#g' 1.txt

10. Delete all digits: sed 's/[0-9]//g' 1.txt 11. Delete all digits and colons: sed 's/[0-9]//g' -e 's/[:]//g' 1.txt 12. Add line numbers to each line (using grep -n and sed to strip the colon): grep -n '.*' 1.txt | sed 's/[:]//g' 13. Delete all lower‑case letters: sed 's/[a-z]//g' 1.txt Replace all lower‑case letters with A : sed 's/[a-z]/A/g' 1.txt 14. Remove every character that is not a lower‑case letter (i.e., keep only a‑z): sed 's/[^a-z]//g' 1.txt 15. Remove every character that is not a digit (i.e., keep only 0‑9): sed 's/[^0-9]//g' 1.txt 16. Convert all lower‑case letters to upper‑case: sed 's/[a-z]/\u&/g' 1.txt 17. Convert all upper‑case letters to lower‑case: sed 's/[A-Z]/\L&/g' 1.txt 18. Combine -n with substitution to print only the modified line, e.g., upper‑casing the first line: sed -n '1s/[a-z]/\u&/g'p 1.txt 19. Swap two fields separated by colons using extended regular expressions: sed -r 's/([^:]+)(:.*:)([^:]+$)/\3\2\1/' 1.txt Example swapping aa and ff in aa:bb:cc:dd:ee:ff yields ff:bb:cc:dd:ee:aa . 20. Perform the same swap with basic regular expressions (escaping parentheses): sed 's/\(aa\)\(.*\)\(ff\)/\3\2\1/' 1.txt 21. Use the -i option for in‑place editing (remember to back up the file first): sed -r -i 's/([^:]+)(:.*:)([^:]+$)/\3\2\1/' 1.txt

command lineunixregular expressionstext processingshell scriptingsed
Practical DevOps Architecture
Written by

Practical DevOps Architecture

Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.