Ultimate Sed Tutorial With Examples – POFTUT

Ultimate Sed Tutorial With Examples


Sed is a stream editor. Sed is used to transform text according to different needs. Sed has a scripting language set too. Sed scripts can be run against files as we see our examples below.  Now we should start an example because this tutorial is one of the longest tutorials on this site.

sed Command Syntax

Sed has following syntax where input files are provided as the last option.

sed [OPTION]... {script-only-if-no-other-script} [input-file]...

sed Command Man Page

More help about sed can get with pageman like below.

$ man sed
Sed Help
Sed Help

Or by just issuing the commandsed in the bash without a parameter will print main help information.

$ sed
Sed Help
Sed Help

Example Text

During the tutorialsed, we will need an example text file where we use commands and options in this text file. We name this text file as likemytext.txt below.

1. poftut.com 
2. ismailbaydan.com
3. default-password.net
This is another line of text

Print Matched Lines

We want to print the lines those matched given string. In this example, we will print the lines with bypoftut using commandp.

$ sed -n  '/poftut/p' mytext.txt

Print Specified Line Numbers

Sed provides support to print only given lines according to their numbers. We will print line 3 with commandp in this example.

$ sed  -n '3p' mytext.txt

Print Specified Line Range According Numbers

In the previous example, we have only printed a single line. We can also print multiple lines by providing the start and end numbers of lines. In this example, we will print lines between 2 and 4 .

 $ sed  -n '2,4p' mytext.txt
Print Specified Line Range According Numbers
Print Specified Line Range According Numbers

Replace Text

One of the most used features of the sed is replacing text. We will use s/foo/bar/g command. s/ is used to search foo/ and g is used to replace /var with matches of search. Or simply this will replace foo with bar . In this example, we will replace poftut with POFTUT

$ sed 's/poftut/POFTUT/g' mytext.txt
Replace Text
Replace Text

Delete Matched Word

In the text, we can delete the matched words we have given. This is similar to substitute where we do not provide replacement words. In this example, we will remove the word poftut .

$ sed 's/poftut//g' mytext.txt
Delete Matched Word
Delete Matched Word

Delete Matched Lines

Similar to the previous example we can delete a word matched line as a whole. In this example, we will delete lines where those contains poftut with the d operation

$ sed  '/poftut/d' mytext.txt
Delete Matched Lines
Delete Matched Lines

Put New Line After Each Line

We may need to create a new line after each line to the end of the file. We can use G for this operation.

$ sed 'G' mytext.txt
Put New Line After Each Line
Put New Line After Each Line

LEARN MORE  PHP Glob() Function To Match Path, Directory, File Names with Examples

3 thoughts on “Ultimate Sed Tutorial With Examples”

Leave a Comment