authors: [“Luke Rawlins”]
date: 2016-12-27
draft: false
title: Search and replace with Vim and Sed
description: “Using search and replace is a great way to save time when editing large files in Linux.”
url: /search-and-replace-with-vim-and-sed/
tags:
– search and replace
– sed
– VIM
Using search and replace is a great way to save time when editing large files in Linux.
Becoming proficient with this task will increase your efficiency and will reduce your time spent doing tedious and error-prone file edits by hand.
For the sake of this tutorial, I'm going to use a copy of the /etc/apt/sources.list file to illustrate some of the changes that we can make.
From your users home directory copy the sources.list file to your home directory. Or you can copy the contents of my list, shown a bit further down, into a new file.
This tutorial assumes that you are comfortable reading and writing files with vim. If not open a terminal and type: vimtutor
sudo cp /etc/apt/sources.list /home/luke/sources.txt
sudo chown $USER:$USER sources.txt
Here are the contents of my sources.list file.
cat sources.txt
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial main restricted
deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial main restricted
## Major bug fix updates produced after the final release of the
## distribution.
deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial-updates main restricted
deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial-updates main restricted
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial universe
deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial universe
deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial-updates universe
deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial-updates universe
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial multiverse
deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial multiverse
deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial-updates multiverse
deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial-updates multiverse
## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu xenial-security main restricted
deb-src http://security.ubuntu.com/ubuntu xenial-security main restricted
deb http://security.ubuntu.com/ubuntu xenial-security universe
deb-src http://security.ubuntu.com/ubuntu xenial-security universe
deb http://security.ubuntu.com/ubuntu xenial-security multiverse
deb-src http://security.ubuntu.com/ubuntu xenial-security multiverse
Search and Replace with Vim
In vim, you can use the :substitute
command, which is pretty much always abbreviated as just :s
followed by a pattern that is separated by forward slashes like this. :s/find/replace/
. This command tells vim to search the current line for “find” and replace it with with “replace”.
To perform a search of all lines change :s
to :%s
. The % in front of “s” tells Vim that we want to search every line and will replace the first instance of our search term with our replace term as this example will show.
vim sources.txt
:%s/xenial/yakkety/
Pressing enter will show give you a summary of how many changes are being made. You can review the file for accuracy and then save the file or quit without saving. Notice that every line is searched.
Vim global search and replace
To change every instance of a word and not just the first instance on a line we need to add the global option to our command.
vim sources.txt
:%s/ubuntu/OLD YELLER/g
Here you can see that every instance of “ubuntu” has been changed to “OLD YELLER”.
If you wanted to find and delete a word you can use the format %s/search term//
leaving the replace field empty. For example to remove comment's from the file.
:$s/#//
Next we'll see how all this is done without opening an interactive file editor.
Sed – Stream Editor
The command syntax for sed
actually isn't much different from vim. By default sed
will output to standard output and will not make changes to a file unless you specify options to allow it to save changes.
Similar to our vim example to change every occurrence of xenial to yakkety:
sed 's/xenial/yakkety/' sources.txt
sed global edit
As with vim, you can use the “g” option to make global changes. By default sed
will only change the first instance of a search term unless you append g
to the end of the search line.
sed 's/ubuntu/OLD YELLER/g' sources.txt
Deleting words from a file works the same way as vim as well.
sed 's/ubuntu//g' sources.txt
sed with in-place editing
You can make changes to files with sed-i
. Using the -i
option will create a backup copy of the original file before making edits, which will save you from inadvertently ruining a working configuration.
sed -i.bak 's/xenial/yakkety/' sources.txt
Learning to use search and replace in sed
and vim
will make your life as a Linux Administrator far easier and is well worth the time you will spend becoming comfortable with using these tools.