DEV Community

Cover image for Most needed commands for "SED" and "Vim" in linux.
VISHAK
VISHAK

Posted on • Updated on

Most needed commands for "SED" and "Vim" in linux.

SED

SED command in UNIX stands for stream editor and it can perform lots of functions on file like searching, find and replace, insertion or deletion. Though most common use of SED command in UNIX is for substitution or for find and replace. By using SED you can edit files even without opening them, which is much quicker way to find and replace something in file, than first opening that file in VI Editor and then changing it.

Let's explore some powerful commands

sed options,

-n is for viewing the changes(will not update the file with the changes).

-i is used to update the changes.

1) How to delete a specific word in a file

# sed -i 's/devops//g' filename.txt
It will delete the word "devops" from the file filename.txt

2) How to change word in a file

# sed -i 's/centos/ubuntu/g' filename.txt
All word changes from centos to ubuntu

3) How to delete a file containing specific word

# sed -i '/_linux_/d' file.txt

It will delete the line containing the word "linux"

4) How to list all the specific words in a file

# sed -n '/_docker_/p' filename.txt
It will list all the lines containing word docker

5)How to delete matching line and 2 lines after matching line

# sed -i '/_NAME_/,+2d' filename.txt

6) How to replace all uppercase characters of the text with lowercase characters

# sed -i 's/\(.*\)/\L\1/ filename.txt'

7) How to view only a specific range of lines in a file

sed -n '5,10p' filename.txt

8) How to replace words inside range of line

# sed -i '60,80 s/centos/ubuntu/g'
Centos will change to ubuntu between lines 60-80

9) How to insert 5 spaces to the left of every lines

# sed -i 's/^/ /' filename.txt

10) how to delete particular line using sed

sed -i '10d' filename.txt It will delete 10th line

VIM

Vim is a text editor for Unix that comes with Linux, BSD, and macOS. It is known to be fast and powerful, partly because it is a small program that can run in a terminal (although it has a graphical interface). It is mainly because it can be managed entirely without menus or a mouse with a keyboard.

Let's explore some powerful commands

1) How to delete all lines in a file
Inside the vim editor.

dG

2) How to delete a specific line

dd

3) How to delete range of lines

:3,5d
It will delete lines from line number 3 to line
number 5

4) How to delete current line to end of line

:.,$d

5) How to delete current line to beginning of line

dgg

6) How to remove the next three lines from the current line

3dd - delete 3 lines
5dd - delete 5 lines

7) How to search a specific word in vim

/string_name

8) How to get to a specific line

:_line_number_
eg- :50 It will redirect to line 50

9) Save a file

:wq!

10) exit without saving a file

:q!

Enjoy Learning🚀🐧

Top comments (0)