How to use SED stream editor

How to use SED stream editor
How to use sed

A Stream EDitor ( SED ) tool is used to filter text that it gets from a pipeline feed. You do not need to interact with the editor while it is running; that is why sed is sometimes called a batch editor. This feature allows use of editing commands in scripts, greatly easing repetitive editing tasks. When facing replacement of text in a large number of files, sed is a great help.The sed program can perform text pattern substitutions and deletions using regular expressions, like the ones used with the grep command.

 

The editing commands are similar to the ones used in the vi editor:

Sed editing commands
Command Result
a\ Append text below current line.
c\ Change text in the current line with new text.
d Delete text.
i\ Insert text above current line.
p Print text.
r Read a file.
s Search and replace text.
w Write to a file.

Apart from editing commands, you can give options to sed. An overview is in the table below:

Sed options
Option Effect
-e SCRIPT Add the commands in SCRIPT to the set of commands to be run while processing the input.
-f Add the commands contained in the file SCRIPT-FILE to the set of commands to be run while processing the input.
-n Silent mode.
-V Print version information and exit.

The sed info pages contain more information; we only list the most frequently used commands and options here.

To search for a word “akm” in a file and print only the result, use -n option , and use /p to print the result.
*If you don’t use -n , it will print all the file , and print the lines that have our result twice , it is not something we may need.

Th3-Gam3 ~ # sed -n '/akm/p' /etc/passwd
akm:x:1000:1000:Falcon,,,:/home/akm:/bin/bash
Th3-Gam3 ~ #

To delete the lines that have our search matches , replace /p with /d.

Th3-Gam3 ~ # cat sed.txt 
This is sed test file.
it was created by akm
akm is a linux engineer
he loves security.

Th3-Gam3 ~ # sed '/security/d' sed.txt 
This is sed test file.
it was created by akm
akm is a linux engineer

Th3-Gam3 ~ # cat sed.txt 
This is sed test file.
it was created by akm
akm is a linux engineer
he loves security.

Th3-Gam3 ~ #

*That didn’t delete it, just exclude it from result displaying .

To exclude range of lines from 1 to 3 from a file, and display the results :

Th3-Gam3 ~ # sed '1,3d' sed.txt 
he loves security.

To exclude all from 3 to the end ( print first tow only):

Th3-Gam3 ~ # sed '3,$d' sed.txt 
This is sed test file.
it was created by akm

To print the first line containing the pattern akm, up to and including the next line containing the pattern security:

Th3-Gam3 ~ # cat sed.txt 
This is sed test file.
it was created by akm
akm is a linux engineer
he loves security.
Th3-Gam3 ~ # sed -n '/akm/,/security/p' sed.txt 
it was created by akm
akm is a linux engineer
he loves security.
Th3-Gam3 ~ #
Find and replace with sed

To find and replace the first matching word to our search , use sed ‘s/word/newWord/’ fileName

To replace all matching words at once, use sed ‘s/word/newWord/g’ fileName

Example: replace all “akm” with “master

Th3-Gam3 ~ # sed 's/akm/master/g' sed.txt 
This is sed test file.
it was created by master
master is a linux engineer
he loves security.
Th3-Gam3 ~ # cat sed.txt 
This is sed test file.
it was created by akm
akm is a linux engineer
he loves security.
Th3-Gam3 ~ #

To display a text at beginning (add “SED ” at begin of every line ):

Th3-Gam3 ~ # sed 's/^/SED /' sed.txt
SED This is sed test file.
SED it was created by akm
SED akm is a linux engineer
SED he loves security.
Th3-Gam3 ~ #

To display a text at the end of a file (add “ SED” at end of every line ):

Th3-Gam3 ~ # sed 's/$/ SED/' sed.txt
This is sed test file. SED
it was created by akm SED
akm is a linux engineer SED
he loves security. SED
Th3-Gam3 ~ #

To do multiple sed tasks at same line use -e before each pattern :

Th3-Gam3 ~ # sed -e 's/$/ SED/' -e 's/^/SED /' sed.txt
SED This is sed test file. SED
SED it was created by akm SED
SED akm is a linux engineer SED
SED he loves security. SED
Th3-Gam3 ~ #
Summary

The sed stream editor is a powerful command line tool, which can handle streams of data: it can take input lines from a pipe. This makes it fit for non-interactive use. The sed editor uses vi-like commands and accepts regular expressions.

The sed tool can read commands from the command line or from a script. It is often used to perform find-and-replace actions on lines containing a pattern.

 

 

One comment on “How to use SED stream editor”

Comments are closed.