Tuesday, April 5, 2022

Linux Sed Command

 Sed (Stream Editor) command.

It performs a lot of functions files like searching,finding,replacing,insertion,deletion without opening a file.


Lab

Create a file called myfile.txt and add the following data

linux is good OS and Open source

unix is not open source

unix has its eduction version minix

linux has different distributions

Replace unix with linux in myfile.txt

sed 's/unix/linux/' myfile.txt

In this case, unix word is replaced with linux word but only first occurrence in each line

Replace unix with linux with nth occurrence of a pattern in a line

sed 's/unix/linux/2' myfile.txt

Replace unix with linux with all the occurrence of a pattern in a line

sed 's/unix/linux/g' myfile.txt

Replace unix with linux from nth occurrence to all the occurrences of a pattern in a line

sed 's/unix/linux/2g' myfile.txt

Replace unix with linux in a specific line

sed '3 s/unix/linux/g' myfile.txt

Deleting lines from a particular file.

sed '3d' myfile.txt

sed '2,4d' myfile.txt



0 comments:

Post a Comment