sed Linux Commands

What is Linux sed Command?



sed is a stream editor. sed command helps to edit or delete all occurrences of one string to another within a file. It takes a file as input and prints the result on screen or redirects the output to a specified file.


sed [options] '{command}' [filename]


Command and its function
-n do not output the trailing newline
-e enable interpretation of the backslash-escaped characters listed below
-E disable interpretation of those sequences in STRINGs

Without -E, the following sequences are recognized and interpolated:
NNN the character whose ASCII code is NNN (octal)
a alert (BEL)
b backspace
c suppress trailing newline
f form feed
n new line
r carriage return
t horizontal tab
v vertical tab


Lets assume that we have a file file1.txt and it has the following data.
hscripts has many valuable free scripts
It is the parent site of
hscripts include free tutorials and free gif images
free DNS lookup tool
Purchase scripts from us
A webmaster/web master resource website
  1. sed G file1.txt>file2.txt

    In the above example, using the sed command with G would double space the file file1.txt and output the results to the file2.txt.
  2. sed = file1.txt | sed 'N;s/n/. /'

    In the above example, sed command is used to output each of the lines in file1.txt with the line number followed by a period and a space before each line.
  3. sed 's/scripts/javascript/g' file1.txt
    Opens the file file1.txt and searches for the word 'scripts' and replaces every occurrence with the word 'javascript'.
  4. sed -n '$=' file1.txt
    The above command count the number of lines in the file1.txt and output the results.

Ask Questions

Ask Question