Skip to content

How to use sed command in Linux

sed, short for “stream editor”, allows you to filter and transform text. A stream editor is used to perform basic text transformations on an input stream (a file, or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors.

How sed Works

sed maintains two data buffers: the active pattern space, and the auxiliary hold space. Both are initially empty.
sed operates by performing the following cycle on each line of input: first, sed reads one line from the input stream, removes any trailing newline, and places it in the pattern space. Then commands are executed; each command can have an address associated to it: addresses are a kind of condition code, and a command is only executed if the condition is verified before the command is to be executed.

When the end of the script is reached, unless the -n option is in use, the contents of pattern space are printed out to the output stream, adding back the trailing newline if it was removed. Then the next cycle starts for the next input line.


sed Options… [Script] [InputFile…]

-n, –quiet, –silentSuppress automatic printing of pattern space.
-e script, –expression=scriptAdd the script script to the commands to be executed.
-f script-file, –file=script-fileAdd the contents of script-file to the commands to be executed.
–follow-symlinksFollow symlinks when processing in place.
-i[SUFFIX], –in-place[=SUFFIX]Edit files in place (this makes a backup with file extension SUFFIX, ifSUFFIX is supplied).
-l N, –line-length=NSpecify the desired line-wrap length, N, for the “l” command.
–POSIXDisable all GNU extensions.
-r, –regexp-extendedUse extended regular expressions in the script.
-s, –separateConsider files as separate rather than as a single continuous long stream.
-u, –unbufferedLoad minimal amounts of data from the input files and flush the outputbuffers more often.


#Replacing word in a file and save the output in same file.
       sed -i ‘s/blogger/blog/g’ test_sed.txt

sed command
You may want to see
How to sort contents of a file

The sort command is used to sort the contents of a file. You can sort the data in a text Read more

How to use crontab in Linux

Cron is the system process that will automatically perform tasks for you according to a set schedule. The schedule is Read more

How to extract archive file in Linux

To extract archive file in Linux, you can use the tar command, used for extracting content from the archive files. "tar" Read more

How to Change permission of a file in Linux

chmod command is used to change the permission of a file in Linux. In Linux, there is a set of rules for Read more

Leave a Reply

Your email address will not be published.