×

sed Command in Linux/Unix with Examples

The 'sed' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command stands for "stream editor" and it works by reading text from a file or input stream, applying a set of commands or "sed script" to the text, and then writing the modified text to the output. The basic syntax for the 'sed' command is:

sed [options] 'script' [file/input-stream]

Where "options" are any optional flags or settings that you want to use, "script" is the set of commands or "sed script" that you want to apply to the text, and "file/input-stream" is the file or input stream that you want to read the text from.

One of the most basic and common uses of 'sed' command is search and replace. For example, if you want to replace all occurrences of the word "old" with "new" in a file named 'file.txt', you would use the command:

sed 's/old/new/g' file.txt

The above command replaces all occurrences of the word "old" with "new" in the file 'file.txt'. The 's' stands for substitute, the first 'old' is the word that we want to replace and the second 'new' is the word that we want to replace it with. The 'g' at the end is a flag that tells sed to perform the replacement globally, i.e. on all occurrences in the line.

Another common usage of 'sed' command is printing specific lines of a file. For example, if you want to print only the first 10 lines of a file 'file.txt', you would use the command:

sed -n '1,10p' file.txt

The above command prints only the lines 1 to 10 of the file 'file.txt'

The 'sed' command also provides options to delete specific lines from a file. For example, if you want to delete the first 10 lines of a file 'file.txt', you would use the command:

The above command deletes lines 1 to 10 of the file 'file.txt'. The 'd' command tells sed to delete the selected lines. In addition to these basic examples, the 'sed' command also supports more advanced operations such as regular expressions, multi-line operations and more.

You can use regular expressions to search for specific patterns in the text, and use commands like 'a' (append) and 'i' (insert) to add new text or 'c' (change) to replace matched lines. You can also use the 'sed' command to perform multi-line operations by using the 'N' command which appends the next line to the pattern space and 'D' command which deletes the first line of the pattern space.

It's also worth mentioning that, by default, 'sed' command writes the changes to the standard output, which means the original file remains unchanged. To make changes in the original file, you can use the '-i' option which allows you to edit the file in place.

It's important to note that, when using the 'sed' command, it's important to be careful with the script you're using. A small typo or mistake in the script can cause unexpected results and even data loss. It's also important to test the command and script on a small set of data before applying it to a large set of data.

In conclusion, the 'sed' command is a powerful and versatile text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. It can be used to perform simple tasks such as search and replace, as well as more advanced tasks like multi-line operations and regular expressions. It's an important command that should be used carefully and with appropriate permissions. With a good understanding of how the command works and its implications, you will be able to efficiently and effectively manage and process text on your Linux systems.


Related Topics

grep Command in Linux/Unix with Examples

The grep command in Linux is a command-line utility that searches for patterns in text files. It stands for "global regular expression print" and it is used to search for...

5 minutes read.

telnet Command in Linux/Unix with Examples

The telnet command in Linux is a command-line utility that allows you to connect to a remote host using the Telnet protocol. Telnet is a network protocol that provides a...

3 minutes read.

curl Command in Linux/Unix with Examples

The 'curl' command in Linux is a command-line tool that allows you to transfer data from or to a server using various protocols, including HTTP, HTTPS, FTP, and many more....

3 minutes read.

top Command in Linux/Unix with Examples

The 'top' command in Linux is a real-time system monitoring tool that displays information about the running processes and system resource usage. The command provides a dynamic, scrolling view of...

4 minutes read.

cat Command in Linux/Unix with Examples

The cat command in Linux is a command-line utility that is used to display the contents of a text file. It stands for "concatenate" and it is used to view...

4 minutes read.

Linux rmdir command

The default use case of the rmdir command in the Linux system is to delete an empty directory. The below screenshot shows how to delete the empty directory. First, we...

1 minute read.

addr2line Command in Linux with Examples

Use of addr2line Command The addr2line command is used to convert addresses to line numbers and file names. This command uses debugging info to check which line number and file name...

4 minutes read.

Linux ls

The primary use case of the ls command is to list the content available in the directory. The default directory is the current directory of the user. The below screenshot...

5 minutes read.

sudo Command in Linux/Unix with Examples

The sudo command in Linux is used to run a command or set of commands with the privileges of another user, typically the root user. This allows users to perform...

3 minutes read.

pwd Command in Linux/Unix with Examples

The "pwd" command in Linux (and other Unix-like operating systems) stands for "print working directory." When executed, it simply displays the current directory (or folder) that the user is currently...

3 minutes read.

echo Command in Linux/Unix with Examples

The "echo" command in Linux is used to display a line of text on the terminal. It can also be used to print the value of a variable or to...

3 minutes read.

chmod Command in Linux/Unix with Examples

The chmod command in Linux is a command-line utility that is used to change the permissions of files and directories. It stands for "change mode" and it is used to...

4 minutes read.

Linux Commands with Examples

Linux Commands with Examples All the Linux commands are executed on the terminal provided by the Linux system. This terminal is a command-line interface to implement and display command outputs. Linux...

10 minutes read.

du Command in Linux/Unix with Examples

The "du" command in Linux (and other Unix-like operating systems) is used to estimate the space used by a file or directory. The du command stands for "disk usage," and...

4 minutes read.

touch Command in Linux/Unix with Examples

The "touch" command in Linux (and other Unix-like operating systems) is used to create new empty files or update the timestamps of existing files. The touch command is a simple...

4 minutes read.

Linux Absolute and Relative Path

The concept of the absolute and relative path is very simple. The names “relative path” and “absolute path” give us little understanding of the concept we are learning here. The...

1 minute read.

How to change the ApacheHTTP port in Linux

What is an Apache HTTP server in Linux? When we talk about the most commonly used web servers, one of the web servers that cannot be missed is Apache. With the...

4 minutes read.

wget Command in Linux/Unix with Examples

The wget command is a command-line utility in Linux that is used to download files from the internet. It supports various protocols including HTTP, HTTPS, and FTP and can be...

5 minutes read.

Top 20 Linux Commands

1) clear command clear is a standard operating system command which is used to clear the terminal screen. This command brings the command line on the top of the terminal. By pressing the...

10 minutes read.

find Command in Linux/Unix with Examples

The find command in Linux is a command-line utility that is used to search for files and directories in a file system. It is a versatile command that can be...

5 minutes read.