sed

stream editor for filtering and transforming text

Syntax

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

Description

Sed is a stream editor. 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.

Options

-e script, --expression=script
       add the script to the commands to be executed

-f script-file, --file=script-file
       add the contents of script-file to the commands to be executed

--follow-symlinks
       follow symlinks when processing in place

-i[SUFFIX], --in-place[=SUFFIX]
       edit files in place (makes backup if extension supplied)

-c, --copy
       use copy instead of rename when shuffling files in -i mode

-r, --regexp-extended
       use extended regular expressions in the script.

Commands

Zero-address ‘‘commands’’
       : label
              Label for b and t commands.

       #comment
              The comment extends until the next newline.

       }      The closing bracket of a { } block.

   Zero- or One- address commands
       =      Print the current line number.

       a \

       text   Append text, which has each embedded newline preceded by a backslash.

       i \

       text   Insert text, which has each embedded newline preceded by a backslash.

       q [exit-code]
              Immediately  quit  the sed script without processing any more input, except that if auto-print is not disabled the current pattern space will be printed.

       Q [exit-code]
              Immediately quit the sed script without processing any more input.

       r filename
              Append text read from filename.

       R filename
              Append a line read from filename.  Each invocation of the command reads a line from the file.

   Commands which accept address ranges
       {      Begin a block of commands (end with a }).

       b label
              Branch to label; if label is omitted, branch to end of script.

       t label
              If a s/// has done a successful substitution since the last input line was read and since the last t or T command, then  branch to label; if label is omitted, branch to end of script.

       T label
              If no s/// has done a successful substitution since the last input line was read and since the last t or T command, then branch to label; if label is omitted, branch to end of script.

       c \

       text   Replace the selected lines with text, which has each embedded newline preceded by a backslash.

       d      Delete pattern space. Start next cycle.

       D      Delete up to the first embedded newline in the pattern space. Start next cycle, but skip reading from the input if there is still data in the pattern space.

       h H    Copy/append pattern space to hold space.

       g G    Copy/append hold space to pattern space.

       x      Exchange the contents of the hold and pattern spaces.

       l      List out the current line in a ‘‘visually unambiguous’’ form.

       l width
              List out the current line in a ‘‘visually unambiguous’’ form, breaking it at width characters.  This is a GNU extension.

       n N    Read/append the next line of input into the pattern space.

       p      Print the current pattern space.

       P      Print up to the first embedded newline of the current pattern space.

       s/regexp/replacement/

       y/source/dest/
              Transliterate the characters in the pattern space which appear in source to the corresponding character in dest.

Addresses

       number Match only the specified line number.

       first~step
              Match every step’th line starting with line first. first can be zero; in this case,  sed operates as if it were equal to step.

       $      Match the last line.

       /regexp/
              Match lines matching the regular expression regexp.

       cregexpc
              Match lines matching the regular expression regexp. The c may be any character.

       GNU sed also supports some special 2-address forms:

       0,addr2
              Start out in "matched first address" state, until addr2 is found. This is similar to 1,addr2, except that if addr2 matches the very first line of input the 0,addr2 form will be at the end of its range, whereas the 1,addr2 form will still be at the beginning of its range. This works only when addr2 is a regular expression.

       addr1,+N
              Will match addr1 and the N lines following addr1.

       addr1,~N
              Will match addr1 and the lines following addr1 until the next line whose input line number is a multiple of N.

Example 1

Print a line from a file using sed command.

[eleiss@eleiss ~]$ cat employee 
EMPLOYEE_ID	NAME		DESIGNATION
--------------------------------------------
1		Rahul		General Manager
2		Soniya		HR. Manager
3		Kapil		Br. Manager
4		Maya		IT Manager
5		Jaya		Asst. Manager
6		Raja		Director
7		Suresh		Executive Officer
8		Anna		Chief Executive Officer
9		Pranav		Director
10		Chidambaram	Director
[eleiss@eleiss ~]$ sed -n '3p' employee 
1		Rahul		General Manager
[eleiss@eleiss ~]$

p command is used to print lines in sed command. The -n option will not print anything unless an explicit request to print is found.

In this example, the third line from the file "employee" is printed.

Example 2

Print all the lines that match a given pattern.(Simulation of grep command)

[eleiss@eleiss ~]$ sed -n '/Manager/p' employee 
1		Rahul		General Manager
2		Soniya		HR. Manager
3		Kapil		Br. Manager
4		Maya		IT Manager
5		Jaya		Asst. Manager
[eleiss@eleiss ~]$

A pattern can also be given as an address to the sed command. In this example, sed command searches for the pattern "Manager" in the file "employee" and print all the matching lines just like what the grep command does.

Example 3

Print a range of lines from a file using sed command.

[eleiss@eleiss ~]$ cat employee 
EMPLOYEE_ID	NAME		DESIGNATION
--------------------------------------------
1		Rahul		General Manager
2		Soniya		HR. Manager
3		Kapil		Br. Manager
4		Maya		IT Manager
5		Jaya		Asst. Manager
6		Raja		Director
7		Suresh		Executive Officer
8		Anna		Chief Executive Officer
9		Pranav		Director
10		Chidambaram	Director
[eleiss@eleiss ~]$ sed -n '3,5p' employee 
1		Rahul		General Manager
2		Soniya		HR. Manager
3		Kapil		Br. Manager
[eleiss@eleiss ~]$ sed -n '/Rahul/,/Jaya/p' employee 
1		Rahul		General Manager
2		Soniya		HR. Manager
3		Kapil		Br. Manager
4		Maya		IT Manager
5		Jaya		Asst. Manager
[eleiss@eleiss ~]$

In this example first the sed prints lines numbers 3 to 5 from the file "employee". The range is defined by line numbers.

In the second illustration, the range is defined by patterns. The command prints all the lines between the two lines matching the given patterns.

Example 4

Print the last line of a file using sed command.

[eleiss@eleiss ~]$ sed -n '$p' employee 
10		Chidambaram	Director
[eleiss@eleiss ~]$

"$" is a special character denoting the last line of the given stream.

Example 5

Print all the lines but not the last one using sed command.

[eleiss@eleiss ~]$ sed -n '$!p' employee 
EMPLOYEE_ID	NAME		DESIGNATION
--------------------------------------------
1		Rahul		General Manager
2		Soniya		HR. Manager
3		Kapil		Br. Manager
4		Maya		IT Manager
5		Jaya		Asst. Manager
6		Raja		Director
7		Suresh		Executive Officer
8		Anna		Chief Executive Officer
9		Pranav		Director
[eleiss@eleiss ~]$

"!" is used for inversion. In this example all the lines are displayed but not the last line from the file "employee". The "$" means the last line and the "inversion".

Example 6

Print multiple ranges of lines from a file using sed command.

[eleiss@eleiss ~]$ sed -n -e '1,5p' -e '$p' employee 
EMPLOYEE_ID	NAME		DESIGNATION
--------------------------------------------
1		Rahul		General Manager
2		Soniya		HR. Manager
3		Kapil		Br. Manager
10		Chidambaram	Director
[eleiss@eleiss ~]$

-e option is used in order to provide multiple commands to the sed command. This example prints 1 to 5 lines and the last line of the file "employee".

Example 7

Demonstrate searching and replacing in sed command.

[eleiss@eleiss ~]$ echo "Sunil is the highest run scorer Indian batsman in test cricket matches." | sed 's/Sunil/Sachin/'
Sachin is the highest run scoring Indian batsman in test cricket matches.
[eleiss@eleiss ~]$

s is for substitution in sed command. "/" is used as a delimiter. Any other character can also be used as a delimiter instead of it.

This example replaces the name "Sunil" by "Sachin" in the given stream.

The search pattern is on the left side and the replacement string is on the right side.

Example 8

Repair a typo in a program file using sed command.

[eleiss@eleiss ~]$ cat process.c 
#inculde<stdio.h>
#inculde<sys/types.h>
#inculde<unistd.h>

int main(int argc,char **argv)
{
	pid_t pid;
	pid=fork();
	if(pid<0)
	{
		/*fork error*/
	}
	if(pid==0)
	{
		/*child Process area*/
	}
	/*parent area*/
	return 0;
}
[eleiss@eleiss ~]$ sed -i 's:inculde:include:' process.c 
[eleiss@eleiss ~]$ cat process.c 
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>

int main(int argc,char **argv)
{
	pid_t pid;
	pid=fork();
	if(pid<0)
	{
		/*fork error*/
	}
	if(pid==0)
	{
		/*child Process area*/
	}
	/*parent area*/
	return 0;
}
[eleiss@eleiss ~]$

In this example, a very common typo is shown. "Spelling mistakes are one of the common mistakes made by programmers. i.e. "inculde" is written instead of "include" in the above example.

There can be some typo scattered in a program file and there can more than one program file in a source tree. sed is the utility to repair all of them with a single line command. Here -i option tells sed to reflect the changes in the original file.


You may write to resources@eleiss.com for any Linux related queries.

advertisement