Using Sed, A Stream Editor In Linux/Unix

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. 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 the one of the following command –
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 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 a sed command in the terminal –

OPTIONS DESCRIPTION
--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 writing 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 complete list of commands-

COMMANDS DESCRIPTION
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 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 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.

Previous Concepts Of Standard I/O, Pipe And Filters In Linux/Unix
Next How to Fix Windows And Linux Showing Different Time In A Dual Boot System

About author

You might also like

Uncategorized 0 Comments

Process Management In Linux/Unix

0.0 00 A process is an instance of a computer program that is currently running. A program can have multiple instances created when it starts executing which means more than

Uncategorized 0 Comments

File Ownership And Permissions In Linux/Unix

0.0 00 Linux and Unix are multi-user operating systems that means different users can access the operating system at a time and can use system resources simultaneously. So the files

Uncategorized 0 Comments

Package Management In RPM(.rpm) Based Linux Distributions

0.0 00 The yum and dnf are the command line package management utilities that can be used for installation, updating, and removal of a software package. These are used in

0 Comments

No Comments Yet!

You can be first to comment this post!

Leave a Reply