Table of Contents
This tip is about the how to Use sed Command on Linux. So read this free guide, How to Use sed Command on Linux step by step. If you have query related to same article you may contact us.
How to Use sed Command on Linux – Guide
The SED command in UNIX stands for Stream Editor and can perform many functions on files such as find, find and replace, insert or delete. Although the SED command is most commonly used on UNIX to replace or find and replace. Using SED, you can even edit files without opening them, which is much faster to find and replace something in a file than opening that file first in the VI Editor and then changing it. The sed stands for Stream Editor. It reads the specified file and modifies the entry as indicated by a list of sed commands. By default, the input is written to the screen, but you can force a file update.
The Linux ‘sed’ command stands for Stream Editor. It is used to manipulate streams (files) with regular expressions. But this edition is not permanent. It just remains on the screen, but in reality the contents of the file remain the same. It is mainly used for text replacement; Also, it can be used for other text editing operations like insert, delete, find and more. With the sed command we can edit files without opening them. Regular expression support makes it a more powerful tool for text manipulation.
Syntax:
Options:
The following are some command-line options for the sed command:
Applying to the STDIN directory
The sed command is not limited to just manipulating files; also, we can apply it to the STDIN directory.
From the above output we first run the command ‘sed’ on a string ‘class7’ where ‘class’ is changed to ‘jtp’ and 7 to 10. Then we run the command ‘sed’ on a stream ‘msg.txt ‘ where ‘learn’ is converted to ‘study’.
Global Replacement
In the previous example, all ‘learn’ words were not edited in ‘study’. To edit each word we have to use a global substitute ‘g’. It will edit all specified words in a file or string.
Syntax:
Consider the examples below:
removing a line
The ‘d’ option will allow us to remove a complete line from a file. We just need to specify a word from that line with the ‘d’ option and that line will be deleted. But, note that all lines with that same word will be erased. It will run as:
Consider the command below:
The above command will delete the lines with the word ‘jtp’. Consider the output below:
Using the multiple sed command
The ‘-e’ option allows us to run multiple sed commands at once. We can perform more than one sed operation by running the command like:
Consider the command below:
As we can see in the output above, all 'red' words are replaced by 'blue' and all 'yellows' words are replaced by 'black'. We can also separate commands like this:
Reading commands from a file
We can save sed commands in a file and apply them at once to any file. This can be done by specifying the '-f' option as follows:
From the above command, the '
replacing characters
We can use the exclamation point (!) as a string delimiter. For example, we want to replace the bash shell and replace it with the csh shell in “/etc/passwd”. To do this, run the command below:
We can get the same result by running the command below:
limiting the sed
Basic use of the sed command processes the entire file. But we can limit the sed command and specify any line. There are two ways to limit the sed command:
We can provide a number to specify a line as follows:
We can also specify a range of lines. To specify a range of lines, run the command as follows:
Inserting and Appending Text
The 'i' and 'a' flag is used to insert and append text in a file. The 'i' flag will add text before the string, and the 'a' flag is used to add text after the string. Consider the command below:
To append text, run the command as follows:
Modifying Lines
The 'c' flag is used to modify a specific line. To modify a line, run the command as follows:
We can also use a regular expression to update more than one line with the same pattern. Consider the command below:
Character Transformation
The 'y' flag is used to transform characters. Character transformation cannot be limited to specific occurrences. To transform characters, run the command as follows:
Printing line numbers
The '=' sign is used to print the line number. To print the line number, run the command as follows:
The equals sign with the '-n' option specifies the line number that contains a corresponding script. Consider the output below:
Final note
I hope you like the guide How to Use sed Command on Linux. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.