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
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Linux Command Line and Shell Scripting Techniques

You're reading from   Linux Command Line and Shell Scripting Techniques Master practical aspects of the Linux command line and then use it as a part of the shell scripting process

Arrow left icon
Product type Paperback
Published in Mar 2022
Publisher Packt
ISBN-13 9781800205192
Length 552 pages
Edition 1st Edition
Tools
Arrow right icon
Authors (2):
Arrow left icon
Jasmin Redzepagic Jasmin Redzepagic
Author Profile Icon Jasmin Redzepagic
Jasmin Redzepagic
Vedran Dakic Vedran Dakic
Author Profile Icon Vedran Dakic
Vedran Dakic
Arrow right icon
View More author details
Toc

Table of Contents (19) Chapters Close

Preface 1. Chapter 1: Basics of Shell and Text Terminal 2. Chapter 2: Using Text Editors FREE CHAPTER 3. Chapter 3: Using Commands and Services for Process Management 4. Chapter 4: Using Shell to Configure and Troubleshoot a Network 5. Chapter 5: Using Commands for File, Directory, and Service Management 6. Chapter 6: Shell-Based Software Management 7. Chapter 7: Network-Based File Synchronization 8. Chapter 8: Using the Command Line to Find, Extract, and Manipulate Text Content 9. Chapter 9: An Introduction to Shell Scripting 10. Chapter 10: Using Loops 11. Chapter 11: Working with Variables 12. Chapter 12: Using Arguments and Functions 13. Chapter 13: Using Arrays 14. Chapter 14: Interacting with Shell Scripts 15. Chapter 15: Troubleshooting Shell Scripts 16. Chapter 16: Shell Script Examples for Server Management, Network Configuration, and Backups 17. Chapter 17: Advanced Shell Script Examples 18. Other Books You May Enjoy

Going through the advanced Vi(m) settings

In the first part of this chapter, we learned some basic Vim operations, which were moving around, copying and pasting, saving, and exiting. Let's take care of some more advanced operations, such as working with find and replace, regular expressions, and similar concepts.

Getting ready

We need to leave our CLI virtual machine running. If it's not powered on, we need to power it back on.

How to do it…

Finding content in Vim is a multi-step process, and it depends on a couple of things. First, it depends on the direction that we want to take, forward or backward, as there are different key sequences for these operations. Let's open the /root/words file again to find some text:

Vim /root/words

Let's start by finding the word fast. For that to work, we need to use the / character from normal mode, as it tells Vim that we're about to use the search function. So, /fast will search for the words fast forward from our cursor. This is the expected result:

Figure 2.14 – Finding a word in Vim

Figure 2.14 – Finding a word in Vim

If we now press Enter and then the n key, we will search for the next appearance of the word fast. This is the expected result:

Figure 2.15 – The next appearance of the word fast

Figure 2.15 – The next appearance of the word fast

However, if we want to find the 10th appearance of the word fast, we need to either press the correct key sequence or use a regular expression. Let's start with a key sequence, which is going to be (again from normal mode) 10/fast. This is the expected result:

Figure 2.16 – Finding the n-th appearance of a word

Figure 2.16 – Finding the n-th appearance of a word

If we want to find the previous appearance of our word (basically, search backward), we need to press the N key (capital N). This is the expected result:

Figure 2.17 – Finding a word backward from the previous cursor

Figure 2.17 – Finding a word backward from the previous cursor

Let's now do a bit of search and replace. Let's say that we want to find all appearances of the word airplane and change them to metro, starting from the beginning of our file. The key sequence used for that would be gg (to go back to the file beginning) and then :%s/airplane/metro/g, followed by the Enter key. This is the expected result:

Figure 2.18 – Replacing all appearances of a word with another word

Figure 2.18 – Replacing all appearances of a word with another word

This syntax presumes the automatic replacement of all occurrences of the word airplane with the word metro placed anywhere in the file. If we just wanted to replace the first appearance of a string in any line, we need to first find that word by using the /word key sequence. Then, we need to use the :s/word1/word2/ key sequence to only change the first appearance of word1 with word2. Let's use the word airship for that example and change that word to ship. If we type in /airship, followed by the Enter key, Vim will position us to the first next appearance of the word airship. If we then use the :s/airship/ship/ key sequence followed by the Enter key, we should get this result:

Figure 2.19 – Replacing one appearance of a word in a specific line with another word

Figure 2.19 – Replacing one appearance of a word in a specific line with another word

It's a subtle difference, but an important one.

We could also use many more commands in vi – for example, using a dot sign (.). That can be used to repeat the last change made in normal mode, which you might also find to be very useful.

We're going to stop here, as we will cover more advanced text search patterns by using regular expressions in Chapter 7, Network-Based File Synchronization.

How it works…

String replacement in Vim works by using an external command called sed, a stream editor. This command is regularly used by system engineers all over the world to quickly replace simple or complex text patterns of any given file (or multiple files) to another complex text pattern. It uses regular expressions as a basis (explained in Chapter 7, Network-Based File Synchronization), which means that, by default, doing search and replace in Vim is quite powerful, albeit a bit complex, as we need to learn the ins and outs of sed and the way Vim treats it as a plugin.

That being said, most of us focus on the quite powerful part of the last paragraph, as using a Vim/sed combination to quickly replace complex text patterns yields fast and precise results – as long as we know what we're doing, of course.

There's more…

Using these concepts requires a bit of extra reading. So, we need to make sure that we check the following additional links:

You have been reading a chapter from
Linux Command Line and Shell Scripting Techniques
Published in: Mar 2022
Publisher: Packt
ISBN-13: 9781800205192
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