Linux Operating System
Inserting Files :
Let us assume that two people have worked on a project. To cut down on time they have split up writing the report. To combine the finished reports together, they can use the facility provided by 'vi' to insert files into the currently open the document. This can be done by opening the file that contains the first part of the report and positioning the cursor at the end of the report file. After this is done, the 'vi' mode has to be switched to command mode by pressing the 'esc' key.
1. Create a file as first report and insert lines as shown below, save it by pressing wq at the command prompt.
[user1 @linux-mac user1]$cat>>first_report
line1 of report 1
line2 of report 1
line3 of report 1
2. Create a file as second report and insert lines as shown below, save it by pressing by wq at the command prompt.
[user1 @linux-mac user1]$cat>>second_report
This is the second report
Line1 of report2
Line2 of report2
Line3 of report2
Second part of the report
3. Issue the command r second_report at the command report as shown below :
This is the first report
Line1 of report2
Line2 of report2
Line3 of report2
:second report
The first part of the report with the read file command
This is the first report
Line1 of report1
Line2 of report1
Line3 of report1
This is the second report
Line1 of report2
Line2 of report2
Line3 of report2
The combined report
At the last line the command ':r second_report has to be issued where 'r'
Stands for read and the next item to be keyed in is the name of the file to be read in.
Commands For Pattern Searching
Searching for a pattern or a word is one of the most important features of any editor. 'vi' is no exception 'vi' has the ability to search forward as well as backward. To begin searching, we need to first switch to command mode by pressing the 'Esc' key. Now we can use the '/' key followed by the string to be searched for. Using the '/' character does the search from current position to the end of the document. A string in the document can also be searched from the current position to the top of the document by using the '?' character.
List of Searching Commands
Command Purpose
/g : search for a pattern 'g' forward
?g : search for pattern 'g' backwards
/ : repeat the last search forward
? : repeat the last search backward
The search can be repeated by using the '/' character or the '?' character to specify the direction of the search.
String Substitution Commands :
Searching and replacing a specified string can be considered to be an extension of the string searching feature available with the 'vi' editor. The general format of the search and substitute is ':s/<string to be searched>/< string to be replaced with >/'. This command can be used to change the first occurrence of the first string in the current line
4. Create a file called story having the following text
And to, the land beyond the mountain was so beautiful. Robert did not have to think twice about banging up his traveling boots first the carved out his territory by clearing the woods and planting corn, the
5. Open the file story in the vi editor
6. Issue the command :/Robert/Ralph/
And to, the land beyond the mountain was so beautiful and bountiful
Robert did not have to think twice about banging up his traveling boots
First the carved out his territory by clearing woods and planting corn, the
-
-
:s/Rober/Ralph/
Figure : string substitution command given
And to, the land beyond the mountain was so beautiful and bountiful
Ralph did not have to think twice about banging up his traveling boots
First the carved out his territory by clearing the woods and planting corn, the
-
-
-
-
:s/Rober/Ralph/
Figure : string substitution done
The search command can be extended to do a 'search and replace' on all occurrences of the string through out the document.
The command to be executed is ':g/Robert/s/ /Ralph/'
The above command means that 'vi' must globally search for 'Robert' and replace all such occurrences with 'ralph' We find that this command is useful, but if only certain occurrence of the string the need to be changed, then it becomes problematic. 'vi' offers a way in which user interaction can take place and the user can decide to replace the strings.
The command to be given is ':g/Robert/s/ /Ralph/c'.
This means that globally [g] search for 'Robert' and replaced [s] each occurrence with 'Ralph' only after confirmation [c] from the user.
https://youtube.com/channel/UCLZqVRZUkuXIlST8BteqTig
This is the link of my YouTube channel please visit at this link and if you like it don't forget to like, share and subscribe the channel.
Comments
Post a Comment