Do you want to use SED commands in Ubuntu?. Great, but first you must understand the concept of SED. Basically SED is a stream editor, where stream refers to a file. It performs editing operation of a text on a file. It is a very powerful tool which is used to transform text.
SED goes through the input stream line by line, places it in stack pattern and then executes the command one by one. Just have a glance at this article to know how to use SED command in Ubuntu.
Steps to Use SED Command in Ubuntu
Step 1: Let’s see the version of “sed” available on your system, type “sudo sed – -version” and press enter.
Step 2: The output of the above command will be visible to you.
Step 3: Now suppose, if you want to create a project report named as “project.txt” and insert the names of your team member, and at the same time, you are required to change one of the member name like Ishita to Namita and then want to save it to a file “project2.txt”.
So write: “sudo sed s/Ishita/Namita/ project.txt >project2.txt” where “s” is known for the substitute, it can be used with various options.
Step 4: To test whether it is done or not, use the command “echo Ishita | sed s/Ishita/Namita/”, and press enter.
Step 5: Output of the above command is shown below and hence it can be seen, that the name is changed to Namita.
Step 6: Now to have a clear vision, you can use “cat” command to get the full content of a file.
Type “cat filename”, in my case, I have written “cat project2.txt”.
Step 7: For an instance, if you type a message using the “echo” command and then without deleting it, you wish to print only the specific part of that message. So type: echo “hello guys, This is just an example.” | sed ‘s/^.*\(, This is.*\)/\1/g’, I have taken this example to explain you the effect of “sed” with ^. *”.
The expression “^.*” will match the character of the string until the last word.
Leave a Reply