Showing posts with label sed. Show all posts
Showing posts with label sed. Show all posts

Thursday, April 7, 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.


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 education 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


Fluentd

Open-source log data collector > why logs? - for compliance (auditing, company, business) - for security (transparency, monitoring, admin...