50 sed Command Examples with Description
The full form of sed is Stream Editor. sed is a useful text processing feature of GNU/Linux.It is a powerful text-processing tool used in Unix and Linux systems. It reads text from files or input streams, makes modifications based on patterns and rules, and outputs the result all without needing to open a text editor manually. sed is a command-line utility that performs text transformation such as searching, finding and replacing, inserting, or deleting lines in a stream of text (usually a file or command output).
50 sed Command Examples with Description
1. Replace the first occurrence of a word in a line:
sed ‘s/old/new/’ file.txt
2. Replace all occurrences of a word in a line:
sed ‘s/old/new/g’ file.txt
3. Replace only on specific line number:
sed ‘3s/old/new/’ file.txt
4. Replace on a range of lines:
sed ‘2,5s/old/new/g’ file.txt
5. Delete specific line:
sed ‘5d’ file.txt
6. Delete range of lines:
sed ‘2,6d’ file.txt
7. Delete lines matching a pattern:
sed ‘/pattern/d’ file.txt
8. Print specific line:
sed -n ‘3p’ file.txt
9. Print range of lines:
sed -n ‘5,10p’ file.txt
10. Replace only if pattern matches:
sed ‘/match/s/old/new/’ file.txt
11. Insert text before a line:
sed ‘3i\This is inserted line’ file.txt
12. Append text after a line:
sed ‘3a\This is appended line’ file.txt
13. Add text at beginning of every line:
sed ‘s/^/PREFIX: /’ file.txt
14. Add text at end of every line:
sed ‘s/$/ :SUFFIX/’ file.txt
15. Replace text only at the beginning of line:
sed ‘s/^old/new/’ file.txt
16. Replace text only at the end of line:
sed ‘s/old$/new/’ file.txt
17. Substitute text using variable:
sed “s/$var/new/g” file.txt
18. Use multiple expressions:
sed -e ‘s/old/new/g’ -e ‘s/foo/bar/g’ file.txt
19. Replace using file as input:
sed ‘s/foo/bar/g’ input.txt > output.txt
20. Display line numbers with output:
sed = file.txt | sed ‘N;s/\n/ /’
21. Change case to uppercase:
sed ‘s/.*/\U&/’ file.txt
22. Change case to lowercase:
sed ‘s/.*/\L&/’ file.txt
23. Replace only nth occurrence in line:
sed ‘s/old/new/2’ file.txt
24. Delete blank lines:
sed ‘/^$/d’ file.txt
25. Delete leading spaces:
sed ‘s/^ *//’ file.txt
26. Delete trailing spaces:
sed ‘s/ *$//’ file.txt
27. Replace tab with spaces:
sed ‘s/\t/ /g’ file.txt
28. Join two lines:
sed ‘N;s/\n/ /’ file.txt
29. Print only lines matching pattern:
sed -n ‘/pattern/p’ file.txt
30. Replace word only if it’s standalone:
sed ‘s/\<word\>/replacement/g’ file.txt
31. Replace only in lines that do not match pattern:
sed ‘/pattern/!s/old/new/g’ file.txt
32. Show lines before a match:
sed -n -e ‘/pattern/{=;x;1!p;g;$!N;p;D}’ file.txt
33. Replace multiple patterns using sed script:
sed -f script.sed file.txt
34. Comment lines starting with #:
sed ‘s/^/#/’ file.txt
35. Uncomment lines:
sed ‘s/^#//’ file.txt
36. Insert blank line after each line:
sed ‘G’ file.txt
37. Print only even lines:
sed -n ‘n;p’ file.txt
38. Print only odd lines:
sed -n ‘p;n’ file.txt
39. Replace digits with X:
sed ‘s/[0-9]/X/g’ file.txt
40. Remove all digits:
sed ‘s/[0-9]//g’ file.txt
41. Replace multiple spaces with one:
sed ‘s/ */ /g’ file.txt
42. Double-space a file:
sed ‘G’ file.txt
43. Print last line only:
sed -n ‘$p’ file.txt
44. Print first line only:
sed -n ‘1p’ file.txt
45. Replace word ignoring case:
sed ‘s/[Oo]ld/new/g’ file.txt
46. Replace words in-place:
sed -i ‘s/old/new/g’ file.txt
47. Backup original before replacing:
sed -i.bak ‘s/old/new/g’ file.txt
48. Use alternate delimiter instead of /:
sed ‘s|/usr/bin|/usr/local/bin|g’ file.txt
49. Replace newline with space:
sed ‘:a;N;$!ba;s/\n/ /g’ file.txt
50. Replace content between two patterns:
sed ‘/start/,/end/s/foo/bar/g’ file.txt