SED command is stands for stream editor which is used to perform basic text transformations on an input file. It can perform lot’s of function on file like, searching, find and replace, insertion or deletion. The most common use of SED command is for substitution or for find and replace.
Syntax:
sed OPTIONS [SCRIPT] [INPUTFILE]
Some uses of sed command are explained below with examples.
Let us take a file called "test.txt" and work with different examples
---
$ cat test.txt
Linux is a free open source operating system (OS) based on UNIX that was created in 1991 by Linus Torvalds. Users can modify and create variations of the source code, known as distributions, for computers and other devices. The most common use is as a server, but Linux is also used in desktop computers, smartphones, e-book readers and gaming consoles, etc.
A distribution of Linux includes the kernel (the central OS component and the bridge between a software application and its data), system utilities, programs and tools for downloading, installing and uninstalling OS updates.
---
SED command by default does not edit the original or source file for our safety but by using ‘-i’ option source file can be edited.
We can find and replace strings or patterns without opening a file. If we are working with large files which can take a long time to open in Vi editor and take a time to replace existing strings with new strings.We can easily replace the strings in a file using the following SED command without opening file.
$ sed 's/Linux/unix/' test.txt
In the above SED command, first, it performs the search operation for 'Linux' string and replaces with ‘Unix’ for the first occurrence only. Here the “s” specifies the substitution operation. The “/” are delimiters.
If we use the ‘g’ flag along with the above command then SED command will replace all 'Linux' string with Unix.
$ sed 's/Linux/Unix/g' test.txt
SED command with ‘d’ flag used to delete the lines from the files.
SED command to delete the First line from the file,
$ sed '1d' test.txt
SED command to delete the Last line from the file
$ sed '$d' test.txt
SED command to delete the lines from 1,3 in a file
$ sed '1,3d' test.txt
SED command to remove blank lines from a file
$ sed '/^$/d' test.tx
Here “^” symbol represents the starting point of a line and “$” represents end of the line. Whereas “^$” represents the empty lines.
We can run multiple SED commands by using -e option, as shown in the below example
$ sed -e 's/Linux/Unix/' -e 's/OS/Operating System/' test.txt
We can replace a string in multiple range of lines of a file. From the below example, the string will replace in 2nd and 3rd lines.
$ sed '1,2 s/Linux/Unix/' test.txt
Command to print a specific line from a file. Here we can print 4th line.
$ sed -n '4p' test.txt
Viewing a range of lines of a document
$ sed -n '1,3p' test.txt
Viewing the entire file except a given range
$ sed '2,3d' test.txt
Using SED command, we can print each line of a file two times by adding 'p' print flag.
$ sed 'p' test.txt
Duplicate the replaced line
$ sed 's/updates/update/p' test.txt
Print only the replaced lines
$ sed -n 's/updates/update/p' test.txt
We can also insert spaces (blank lines) for each non-empty line in a file. To insert one blank line every other line in a plain text file, do:
$ sed G test.txt
To insert two blank lines, do:
$ sed 'G;G' test.txt
Note: Before editing the source file, take a backup for our safety.