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.