I am Parimal Pradhan, Today we are going to discuss the sed command. In Linux, we can search and replace a word in a file with the help of the sed command. sed stands for stream editor.
Here I have taken a sample text.
Linux is the most popular server OS. Linux is a clone of UNIX. Knowing one is as good as knowing the other. In this UNIX / Linux tutorial for beginners series, we will be using linux as it’s freely available. The training will require you to execute certain commands. Make sure to practice them!
linux.
linux is great os. Linux is opensource.
learn operating system.
linux which one you choose.
Linux is easy to learn.linux is a multiuser os.Learn unix.
Use case-1
Now I want to change the word linux to unix. So let's understand with the below command.
Syntax: sed 's/<old text>/<new text>' <filename>
sed 's/linux/unix/' test1.txt
s => It means it will search the word
Let's see the output:
parim@DESKTOP-F85NG1F MINGW64 ~/oneDrive/Desktop/sed (master)
$ sed 's/linux/unix/' test1.txt
Linux is the most popular server OS. Linux is a clone of UNIX. Knowing one is as good as knowing the other. In this UNIX / Linux tutorial for beginners series, we will be using Linux as it’s freely available. The training will require you to execute certain commands. Make sure to practice them!
unix is great os. Linux is opensource.
learn operating system.
unix which one you choose.
Linux is easy to learn.unix is a multiuser os.Learn unix.
It searches the word linux in small case and it only replaces it with small case unix.
If you observe if L is capital in linux not replaced with unix.
Use case-2
Now Let's understand the second use case where I want to replace Linux words globally in the file with Unix words.
To change globally we need to add only g word after "/".Let's see the command and run it.
sed 's/Linux/Unix/g' test1.txt
Use case-3
How to ignore the case and replace it globally?
i => To ignore case.
g => To search and replace globally.
sed 's/Linux/Unix/ig' test1.txt
Use case-4
Now if we want to see particular lines of code then we can also use the sed command.
sed -n '4p' test1.txt
Please check the sample code.
1.Linux is the most popular server OS. Linux is a clone of UNIX.
2.Knowing one is as good as knowing the other. In this UNIX / Linux tutorial for beginners series, we will be using Linux as it’s freely available.
3.The training will require you to execute certain commands. Make sure to practice them!
4.linux is great os. Linux is opensource.
5.learn operating system.
6.linux which one you choose.
7.Linux is easy to learn.linux is a multiuser os.Learn unix.
output
Use case-5
Now if we want to see a range of lines of code Suppose I want to see the text in a file from line number 2 to 4 then we can use the below command.
parim@DESKTOP-F85NG1F MINGW64 ~/oneDrive/Desktop/sed (master)
$ sed -n '2,4p' test1.txt
Output
Use case-6
How to delete a first single line?
sed '1d' <filename>
parim@DESKTOP-F85NG1F MINGW64 ~/oneDrive/Desktop/sed (master)
$ sed '1d' test1.txt
output
Use case-7
How to remove a range of lines?
Syntax : sed '<start line>,<end line>d' <filename>
parim@DESKTOP-F85NG1F MINGW64 ~/oneDrive/Desktop/sed (master)
$ sed '2,4d' test1.txt
output
Thank You for reading this blog
Parimal Pradhan
You can follow me on LinkedIn for my daily updates:- linkedin.com/in/parimal-pradhan-b62021168
Great initiative by the #trainwithshubham community. Thank you Shubham Londhe