Using Sed, A Stream Editor In Linux/Unix


Sed stands for stream editor It was based on the scripting feature of ed(a line based interactive text editor in Unix). It is used to perform basic text transformation on the input stream either it is from a file or input from a pipeline. The sed was developed in 1973-74 by Lee E. McMahon of Bell Labs. Now it is available for most of the operating systems.


Installing sed in Linux

By default, it comes preinstalled in some Linux distributions. If it is not in your system you can download it by using one of the following commands –
sudo apt-get install sed  (In debian based distribution)
sudo yum install sed (In CentOS and similar distro that uses yum as package manager)
sudo dnf install sed (In fedora)
Check if it is accessible in the terminal by using the command

sed --version


Basic Syntax of sed

sed script inputfile or
sed options script inputfile


Command-Line Options

Given below are some command-line options that are available to use with sed command in the terminal –

OPTIONSDESCRIPTION
--v
--version
 Display the current version of sed and exit
--help Display the command line options in brief and exit
-n
--quiet
--silent
 Suppress automatic printing of pattern space
--debug Print the input sed program in canonical form, and annotate program execution
-e script
--expression = script
 Add the commands in the script to the set of commands to be run while processing input
-f script_file
--file=script_file
 Add the commands contained in the file script_file to the set of commands to be run while
processing the input
-i[SUFFIX]
--in-place[=SUFFIX]
 Edit files in place (makes backup if SUFFIX supplied)
-l N
--line-length=N
 Specify the desired line-wrap for l command. If not specified it s taken to be 70.
--posix Disable all GNU extensions in order to write portable scripts
-b
--binary
 Sed will open input files in binary mode
-E
-r
--regexp-extended
 Use extended regular expressions in the script(for portability use POSIX -E)
-s
--separate
 Consider files as separate rather than as a single, continuous long stream.
-u
--unbuffered
 Loads minimum amounts of data from the input files and flush the output buffer more often
-z
--null-data
 Separate lines by NULL characters

Exit status

After the execution of sed command, it returns zero or non zero value which indicates something. Given below is the status code and error values-

0 -Successful completion

1 -Invalid command, syntax, regular expression or a GNU sed extension command used with--posix

2 -The file(s) that are mentioned in the command line could not be opened. Processing continues with other files.
4 -An I/O error, or other processing error during runtime, GNU sed aborted immediately


Sed Commands

The commands given below are some frequently used sed commands. You can read GNU sed commands for a complete list of commands-

COMMANDSDESCRIPTION
s/regexp/replacement/[flag] (substitute) Match the given string i.e. regexp and replace it with the replacement string
a\ text Append text after a line
c\ text Replace a line with text
d Delete line(s)
e Executes the command that is found in the pattern
F Print the file name of the current input file
i\ text Insert text before a line
P Print the line(s)
p Print lines up to first
q[exit-code] Exit sed without processing any more commands or input
Q[exit-code] Immediately quit the sed script without processing any more command. It is a GNU   extension
r filename Reads file filename
w filename Write to a given file filename
#comment A comment until the next newline
= Print the current input line number with a trailing newline.

An overview of the sed script

A sed program can be consist of one or more sed commands, that are passed on by one or more of the -e, -f, --expression and --file options or the first non-option argument if zero of these options are used.
Sed command syntax:

sed [command(s)] [option(s)]

For example-

sed '4,9d' input_file > output_file
This example will delete lines 4 to 9 in the input_file

Commands within a script file can be separated by semicolons or newlines. And multiple scripts can be specified with -e or -f options
sed '/^lalit/d ; s/he/she/' input_file > output_file
In the example given above two operations of sed are performed together that are deletion and replacement of string. Any line containing the string “lalit” in input_file will be deleted and the word he will be replaced with she.


Some examples of using sed command

1. Insert a blank line after each line-

You can see the contents of file input_file by using cat command. To insert a blank line execute sed 'G' input_file code in your terminal.

2. Delete blank lines after each line-

To delete the inserted blank lines you have to execute sed '/^$/d' input_filecode in your terminal.

3. Insert line number for every line-

To put the line number at the starting of a line you should use the sed = input_file in your terminal.

4. Print only lines from which the pattern matches-

The syntax of the command to print all the lines which match the pattern is sed -n /pattern/p file_name(s)

You can redirect the output of each command that performs some operations on a file by using a redirection operator to a separate file.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.