Learn your SED command — Part 1

Hello All ,

Learning always fun . When we want learn to learn something ,we always search for basics. Basics in the sense ,very simple form a technology/methodology or brief idea. Yes ,especially if you want to learn all the new technologies it is very important learn basics .

So i thought to cover basics of some of new technologies through my blogs . As part of it ,today i am sharing the basics of SED command.



In linux, everything is a file. To create /edit we will use VI editor most. But VI Editor is a interactive (means open file and then give commands and again close) editor. Not only Vi we have other editors like emacs ,kedit ,gedit ,pico and nano but all these are interactive editors.

But some editors are not interactive like

Prepare command ahead of time

Send command to program

Program executes command

And these non-interactive editors will allow you to automate the tasks like advanced text substitutions using regular expressions.


Let me give an example , there a file called “sample” and you need to modify the contents of a file like need to replace a word “crom” into “cron” .  Suppose the word repeats almost 100 times in the file .

What you will do in this case ?

You will go to each word and change it to “cron”.

Imagine how much difficult that will be and time taking too .

So in order to edit the files in Unix we have a special editor called “SED “.

I gave only one example for SED feature. Going forward we can most useful features of SED.

So let we start Learning SED.


What is SED?

Full form of SED is “Stream Editor”. Stream means source or destination of data (e.g.: STDIN, STDOUT, STDERR) and can also refer to the data itself, such as data passed along in a pipeline (input and output streams).

Definition: In reality, Stream EDitor is interpreter for a portable, shell-independent language designed to perform text transformations on streams of data

SED is a non interactive context editor that runs on most Unix systems and operates on a stream of text that it reads from either standard input or from a file. This means that you can send the output of another command directly into SED for editing, or you can work on a file that you’ve already created. You should also be aware that SED outputs everything to standard out by default. That means that, unless redirected, SED will print its output to the screen instead of saving it in a file. SED can be used at the command-line, or within a shell script, to edit a file non-interactively. Perhaps the most useful feature is to do a ‘search-and-replace’ for one string to another.

So where we can use SED?

  • To edit too large files
  • To perform multiple global editing functions efficiently in one pass through the input.
  • To edit any size file when the sequence of editing commands is too complicated to be comfort-ably typed in interactive mode

SED can work in two ways:

  • Read input from file
  • Makes changes to the existing file automatically.
  • Reads from STDIN (Standard input) as a part of pipeline à filter
  • Can also pipe output to STDOUT (Standard Output)

The basic Syntax

sed [options] commands [file name]

File Name – If file name is specified performs operations on the file else performs on STDIN.

Command – Specify sed command you want to perform.

e.g.: sed ‘s/crom/cron/g’ sample – replaces every occurrence of crom into cron in the file called “sample”.

Options – sed supports multiple operations using these options.

e.g.: sed -e – allows multiple edits.

Here after once the data modified by the sed command it will sent to STDOUT (Printed to screen) so if we want to save it into a specific file, what we need to do?

Simple, we all knows about redirection (>,<,>>,<<) right , so simply we will use” > “ to redirect the output to a specific file.

e.g.: sed ‘s/crom/cron/g’ sample > sample1



SED – Options:

  • I think now everyone will get one doubt that sed can’t modify original file?

The answer is yes. Sed can modify the original file.

As we discussed about options above, sed uses -i (inplace)option to modify the contents of the original file .

Eg: sed -i ‘s/crom/cron/g’ sample – Make changes to the original file.

What happens here is after execution of sed command it transfer the output to a temporary file and the copies data from temporary file to original file.


  • In the above the above example, we used only one command at a time with sed, but can we use multiple commands with sed ?

Yes, sed had many features that we can force multiple commands at a time using -e (expression) option.

Let’s take above example again,

If we want to change cron into CRON and deny into DENY at time use -e option before the command.

E.g.: sed -e ‘s/cron/CRON/g’  -e ‘s/allow/ALLOW/g’ -e ‘s/deny/DENY/g’ samplesed2.png

If you want run one command string on multiple lines use “ \ “ as a separator between  “ -e “ option

E.g.: sed -e ‘s/cron/CRON/g’ \

               -e ‘s/allow/ALLOW/g’ \

               -e ‘s/deny/DENY/g’ sample    

  • Similarly sed have many options which I listed few of them below :

SED Options






Suppresses Default output (Suppress automatic printing of pattern space)




Precedes a sed script file name




Separate argument (used for executing one command on multiple files)




Use extended regex syntax




(Help) Used to print the summary of the sed commands




(Version) Will print the version of sed

That’s it for Part 1 . Hope i am explaining in a very basic language using basic terms .

Please do not forgot to provide feedback .

Thanks for reading. Happy Learning


Express your Opinion here

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s