30 examples of the sed command - with regex


Sed is a non-interactive text editor. It comes from English [S]tream [ED]itor, ie text flow editor. GNU Sed is currently used by most Linux distributions and has the most new features: GNU/Linux.

It’s case sensitive

  • -i change the file
  • -e print to screen without changing file
  • -n suppress, show only command output
  • s replaces one piece of text with another
  • ! inverts the logic of the command
  • ; command separator
  • | string separator
  • d at the end delete
  • p at the end prints
  • g at ​​the end (as d and p is used) changes all occurrences
  • q quit sed, does not continue command

Complete list of GNU sed character classes

  • [[: alnum:]] Alphabetic and Numeric [a-z A-Z 0-9]
  • [[: alpha:]] Alphabetic [a-z A-Z]
  • [[: blank:]] Blank character, space, or tab [\ t]
  • [[: cntrl:]] Control characters [\ x00- \ x1F \ x7F]
  • [[: digit:]] Numbers [0-9]
  • [[: graph:]] Any visible character (i.e. except blank) [\ x20- \ x7E]
  • [[: lower:]] Lowercase letters [a-z]
  • [[: upper:]] Capital letters [A-Z]
  • [[: print:]] Visible characters (ie except control characters) [\ x20- \ x7E]
  • [[: punct:]] Score [-! ”# $% & ‘() * +,. /:;? @ [] _` {} ~].
  • [[: space:]] White space [\ t \ r \ n \ v \ f]
  • [[: xdigit:]] Hexadecimal Number [0-9 a-f A-F]

1 - Swap all words in one file for another

sed -i 's/word/other/' file.txt

2 - Prints only the ninth line

sed -n '9p' file.txt

3 - Prints from the sixth line to the ninth line

sed -n '6.9p' file.txt

4 - Delete all lines containing the word string in the file.

sed -i '/dmx/d' file.txt

5 - Put one word at the beginning of each line.

sed 's/^/word/' file.txt

6 - Put a word at the end of each line.

sed 's/$/word/' file.txt

7 - Prints only lines that START with the string ‘http’

sed -n '/^http/p' file.txt

8 - Deletes only lines that START with the string ‘http’

sed -n '/^http/d' file.txt

9 - Exchange ALL occurrences of the word “marcos”, “eric”, “camila” with the word “penguin”

sed 's/marcos\|eric\|camila/penguin/g' file.txt

10 - Exchange everything between the words “Marcos” and “Eric” for the word “they”, for example, the text is:

“On Saturday Marcos went out to bike with Eric, but they didn’t stay up late.” And it will look like this: “On Saturday they didn’t stay up late.”

sed's/Marcos.*Eric/they/' file.txt

11 - Delete blank line and change file

sed -i '/^$/d' file.txt

12 - Replaces “foo” with “bar” only lines containing “plop”

sed '/plop/s/foo/bar/g' file.txt

13 - Replaces “foo” with “bar” except lines containing “plop”

sed '/plop/!s/foo/bar/g 'file.txt

14 - Insert Line 2 with line 7 the “#” at the beginning of each line

sed '2,7s/^/#/' file.txt

15 - Inserts the word ‘NEW’ at the beginning of each line from line 21 to line 28

sed -i '21,28s/^/NEW/' file.txt

16 - Swap everything between the tags “<” and “>” for the word “CODE”:

sed 's/<[^>]*>/CODE/g' file.txt

17 - Prints only first occurrence of line with given string

sed -n '/day/{p;q;}' file.txt

18 - Include text at end of line 9

sed '9s/$/end of line/' file.txt

19 - Put all lines into one

sed ':a;$!N;s/\n//;ta;' file.txt

20 - Replaces the word “BEAUTY” with “YES” only between certain lines.

sed '3,6s/BEAUTY/YES/' file.txt

21 - Deletes what is between the word “spoke” and “second” (delimiters)

sed '/second/{/spoke/{s/second.*spoke//;t};:a;/spoke/!{N;s/\n//;ta;};s/second.*spoke/\n/;}' file.txt

22 - Removes HTML commands

sed 's/<[^>]*>//g' file.txt

23 - Deletes the 1st character of the sentence.

sed 's/.//' file.txt

24 - Deletes the 4th character of the sentence.

sed 's/.//4' file.txt

25 - Deletes the first 4 characters

sed 's/.\{4\}//' file.txt

26 - Deletes at least 4 characters

sed 's/.\{4,\}//' file.txt

27 - Deletes 2 to 4 characters

sed 's/.\{2,4\}//' file.txt

28 - Range Examples

echo "aáeéiíoóuú" | sed "s/[a-u]//g"
áéíóú
echo "aáeéiíoóuú" | sed "s/[á-ú]//g"
aeiou

29 - Transforms text (URL) into HTML link tags.

It was: http://www.com

It is: <a href="http://www.com">http://www.com</a>

sed 's_\<\(ht\|f\)tp://[^ ]*_<a href="&">&</a>_' file.txt

30 - Regular Expressions with SED (sed regex)

This sed reads data from file.txt and erases (command d) from the first line to the line containing 3 numbers in a row, throwing the result on the screen. If you want to save the result, redirect it to another file, not the file.txt itself.

sed '1,/[0-9]\{3\}/d' file.txt

Delete numbers

s/[0-9]\+//g' file.txt

Prints only lines containing SCORE

sed -n '/[[:punct:]]/p' file.txt

Print only lines beginning with Numbers

sed -n '/^[[:digit:]]/p' file.txt

Formatting Phone Number

We have a file with the phone numbers like this:

7184325689
4333285236
1140014004
3633554488

Running some of these command modes in SED:

  • Neanderthal Mode

Replaces 2 characters “..” with “&” which is the output of the request. Performs another sed to replace 8 characters again with “&” Note: You must always escape the parentheses “(“ and “)”

sed 's/../\(&\)/' file.txt | sed 's/......../&-/' file.txt
  • Medieval mode

Same as above, just put the “{8}” to mark 8 characters “.” Also, ALWAYS need to escape the “{“ and “}” keys

sed 's/../\(&\)/' file.txt | sed 's/.\{8\}/&-/' file.txt
  • Modern mode

Instead of throwing the exit, I separated the command with a semicolon “;” and launched another sed “s”

sed 's/../\(&\)/;s/.\{8\}/&-/' arquivo.txt
  • Postmodern mode

This mode is to understand the following

The first command in parentheses “(..)” Then separated by slash
I threw or command in parentheses “\ (.{4 } )” The first command output goes to bar 1 “\1” And the second command for slash 2 “\2” It could also have bar 3, n,…

sed 's/\(..\)\(.\{4\}\)/(\1)\2-/g' arquivo.txt

Stays like this:

(71) 8432-5689
(43) 3328-5236
(11) 4001-4004
(36) 3355-4488

Thanks for read.


sed linux terminal gnu


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles