×

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

Linux Commands List

Linux Commands List The Linux systems are filled with commands for several use cases. We have divided these commands into some of the most common implementations for a better understanding of...

2 minutes read.

Linux cd

Linux cd: As in GUI operating systems like Windows 10, we use the mouse to travel to various folders back and forth; similarly, we have the “cd” command in Linux...

4 minutes read.

ps Command in Linux/Unix with Examples

The 'ps' command in Linux is used to display information about the running processes on a system. The command stands for "process status" and it allows you to view details...

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.

Linux Bash

Linux Bash In this tutorial, we are going to learn what the shell is? And how many various shells we have, we have also covered most of the bash shell features....

4 minutes read.

df Command in Linux/Unix with Examples

The 'df' command in Linux is used to display the amount of disk space available on the file system. The command reports the amount of disk space used and available...

3 minutes read.

wc Command in Linux/Unix with Examples

The wc command in Linux is a command-line utility that is used to count the number of lines, words, and characters in a text file or standard input. The name...

4 minutes read.

addgroup Command in Linux with Examples

What is the addgroup command? It is a command in Linux to create a new group using specified command lined and the system’s default values. How to add groups Adding a user group:...

4 minutes read.

mv Command in Linux/Unix with Examples

The 'mv' command in Linux is used to move or rename files and directories. The command stands for "move" and it allows you to move files and directories from one...

3 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.

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.

chown Command in Linux/Unix with Examples

The 'chown' command in Linux is used to change the ownership of a file or directory. The command stands for "change owner" and it allows you to change the user...

3 minutes read.

rename Command in Linux/Unix with Examples

The rename command in Linux is used to rename multiple files at once, using a specified pattern. The command is also known as rename, ren, and mv. This command is...

3 minutes read.

man Command in Linux/Unix with Examples

The man command in Linux is used to display the manual pages for a given command or system call. These manual pages provide detailed information on the command's usage, options,...

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.

tail Command in Linux/Unix with Examples

The "tail" command in Linux (and other Unix-like operating systems) is used to display the last few lines of a file. By default, the tail command will display the last...

4 minutes read.

Linux mkdir | Linux Create Directory

The Linux mkdir command is very much similar to Windows’s "new folder" feature. This command is used in Linux os to create new directories. The command mkdir stands for "make...

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.

Linux Tutorial for Beginners

What is Linux? Linux is an open source Operating System like Windows, Android, Mac OS, and iOS. An operating system is a software that enables communication between the user and the...

12 minutes read.

ssh Command in Linux/Unix with Examples

The ssh (Secure Shell) command in Linux is used to remotely access and control another computer or device over a network. It provides a secure and encrypted connection between the...

6 minutes read.