Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
CentOS System Administration Essentials

You're reading from   CentOS System Administration Essentials Become an efficient CentOS administrator by acquiring real-world knowledge of system setup and configuration

Arrow left icon
Product type Paperback
Published in Nov 2014
Publisher Packt
ISBN-13 9781783985920
Length 174 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Andrew Mallett Andrew Mallett
Author Profile Icon Andrew Mallett
Andrew Mallett
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Taming vi 2. Cold Starts FREE CHAPTER 3. CentOS Filesystems – A Deeper Look 4. YUM – Software Never Looked So Good 5. Herding Cats – Taking Control of Processes 6. Users – Do We Really Want Them? 7. LDAP – A Better Type of User 8. Nginx – Deploying a Performance-centric Web Server 9. Puppet – Now You Are the Puppet Master 10. Security Central 11. Graduation Day Index

Search and replace

So we are not exactly on a "search and destroy" mission, but if it helps by adding a little enjoyment to our learning, then we can embark upon a search and replace mission. Linux has a huge amount of power available on the command line and nothing less than the stream editor, sed. Even without entering the Vim editor, we can search for and replace text in a single file or even across multiple files. Not having to use an interactive editor opens up more administrative scope to us by being able to script updates across a single or many servers. The functionality we have in the sed command is available to us for use from within Vim or as a standalone application. We will be learning in this subsection how to search for and replace text within files using sed and from within Vim, building skills that we can use across CentOS and other operating systems including OS X on the Mac.

Firstly, let's take a scenario that we have recently changed our company name and we need to change all the references of Dungeons in a text document to Dragons. Using sed, we could run the command directly from the console:

$ sed -i 's/Dungeons/Dragons/g' /path/file

This will read the file line by line, replacing all occurrences of the string Dungeons with Dragons. The -i option allows for in-pace edits, meaning we edit the file without the need to redirect the output from sed to a new file. The g option allows for the replacement to occur across all instances of Dragon even if it appears more than once per line.

To do the same within Vim where we have the file open, run the following command:

:%s/Dungeons/Dragons/g

The percent symbol is used to specify the range as the whole document; whereas if we use the following command, we would only search lines 3 through 12 inclusive of the search string. In this case, the range is said to be lines 3 to 12 whereas with %, the range is the complete document.

:3,12s/Dungeons/Dragons/g

The range can be very useful when perhaps we want to indent some code in a file. In the following line, we again search lines 3 through to 12 and add a Tab to the start of each line:

:s/3,12s/^/\t/

We have set the range in the previous command within Vim to represent lines 3 to 12 again. These lines may represent the contents of an if statement, for example, that we would like to indent. We search first for the carat symbol, ^ (the start of a line), and replace it with a tab (\t). There is no need for the global option as the start of a line obviously only occurs once per line. Using this method, we can quickly add indents to a file as required, and we are again Zen superheroes of Vim.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image