LINUX+ LAB SERIES (LX0-101) Lab 10a: Managing Text Files Document Version: 2015-09-18 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Development was funded by the Department of Labor (DOL) Trade Adjustment Assistance Community College and Career Training (TAACCCT) Grant No. TC-22525-11-60-A-48; The National Information Security, Geospatial Technologies Consortium (NISGTC) is an entity of Collin College of Texas, Bellevue College of Washington, Bunker Hill Community College of Massachusetts, Del Mar College of Texas, Moraine Valley Community College of Illinois, Rio Salado College of Arizona, and Salt Lake Community College of Utah. This workforce solution was funded by a grant awarded by the U.S. Department of Labor's Employment and Training Administration. The solution was created by the grantee and does not necessarily reflect the official position of the U.S. Department of Labor. The Department of Labor makes no guarantees, warranties or assurances of any kind, express or implied, with respect to such information, including any information on linked sites, and including, but not limited to accuracy of the information or its completeness, timeliness, usefulness, adequacy, continued availability or ownership. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files Contents Introduction ........................................................................................................................ 2 Objective ............................................................................................................................. 2 Linux+ LX0-101 Exam Objectives ........................................................................................ 3 Lab Topology ....................................................................................................................... 4 Lab Settings ......................................................................................................................... 5 1 Using the vi Editor for Basic File Editing ..................................................................... 6 1 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files Introduction This lab provides guidance on performing Lab 10a: Managing Text Files of the Linux+ LX0-101 course, using a NETLAB+ system. By performing this lab, students will learn how to manage text files using the vi editor. Objective In this lab, you will perform the following task: Using the vi editor for basic file editing. 2 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files Linux+ LX0-101 Exam Objectives This lab will cover the topics for the following LX0-101 objectives: 103.8 Perform basic file editing operations using vi 1. Navigate a document using vi. 2. Use basic vi modes. 3. Insert, edit, delete, copy, and find text. The following is a partial list of the used files, terms, and utilities: a. vi b. /,? c. h,j,k,l d. l, o , a e. c,d,p,y,dd,yy f. ZZ, :w!, :q!, e! 3 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files Lab Topology 4 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files Lab Settings The information in the table below will be used to complete the lab. Additional details will be provided within the task sections as required. System Username/Password CentOS Server sysadmin/netlab123 Ubuntu Server sysadmin/netlab123 Fedora Workstation sysadmin/netlab123 Ubuntu Workstation sysadmin/netlab123 All Machines root/netlab123 5 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 1 Using the vi Editor for Basic File Editing In this task, we will explore some of the basic text editing features of the vi editor. The vi editor is a powerful text editor with a bit of a learning curve but capable of performing a wide variety of text editing tasks. The vi editor has two modes: insert and command. In insert mode, you add text to a document. In command mode, operations can be performed such as navigation, searching, saving, and exiting the editor. 1. Click on the Fedora Workstation icon in the pod topology to launch the virtual machine. 2. The virtual machine will display a login screen. Make sure sysadmin is in the user field . Enter the password netlab123 and press Enter. 6 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 3. Once you have logged in, a terminal window may appear. If it does not, click on the Kickoff Application Launcher, the “f”, in the lower-left corner of the desktop. In the search bar, type konsole and click on Konsole, which will launch a terminal window: 7 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 4. Create a file that can be used to demonstrate using the vi editor: echo “Welcome to the vi editor.” > myfile echo “It is a very powerful text editor.” >> myfile echo “Especially for those who master it.” >> myfile cat myfile Your output should be similar to the following: If the file exists, the first echo command overwrites its contents. If there is no input data, a new file will be created. 5. Invoke the vi editor to modify the file we created. When vi is invoked we are placed in command mode by default: vi myfile Your output should be similar to the following: On your screen, notice in the bottom-left, the file name, the number of lines, and the number of characters in the file are displayed. 8 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 6. Press each of the following keys two times and observe how the cursor is moved. Remember that we are in command mode: Key Function j Moves cursor down one line (same as down arrow) k Moves cursor up one line (same as up arrow) l Moves cursor to the right one character (same as right arrow) h Moves cursor to the left one character (same as left arrow) w Moves cursor to beginning of next word e Moves cursor to end of word b Moves cursor to beginning of previous word Depending on the connection/terminal type, h, j, k and l always work whereas the arrow keys may not. 7. More vi cursor navigation. Press the following keys and observe how the cursor moves: Keys $ 0 (zero) Function Moves cursor to end of current line Moves cursor beginning of current line 3G Jumps to third line (nG jumps to the nth line) 1G Jumps to first line Shift+G Jumps to the last line 8. Move the cursor to the beginning of the word “very.” by pressing the following: Shift+G: G k 8l The third command is 8 plus lower case L. The cursor should be on the letter v for “very” as shown below: 9 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 9. Delete the word “very”: dw 10. Undo the last operation: u 11. Delete two words: 2dw 12. Undo the last operation: u 10 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 13. Delete four characters, one at a time: xxxx 14. Undo the last 4 operations and recover the deleted characters: 4u 15. Delete 14 characters: 14x 16. Undo the last operation: u 11 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 17. Delete the five characters to the left of the cursor Shift+X: 5X 18. Undo the last operation: u 19. Delete the current line: dd 20. Whatever was last deleted or yanked can be “pasted”. Paste the deleted lines below the current line: p 12 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 21. Undo the last two operations: 2u 22. Delete two lines, the current and the next: 2dd 23. Undo the last operation: u 24. Move to the fourth word then delete from the current position to end of line Shift+D: 4w D The command d$ also deletes to end of line. The $ character, as seen earlier, advances to end of line. Thus d$ deletes to end of line. 13 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 25. Undo the last operation: u 26. Join two lines, the current and the next Shift+J: J 27. Undo the last operation: u 28. Copy, or “yank” the current word: yw 14 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 29. Paste or “put” the copied word before the current cursor: P 30. Undo the last operation: u 31. Move to the first line, then join three lines Shift+G, Shift+J: 1G 3J 32. Undo the last operation: u 15 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 33. Search for and delete the word "text" (add a space after the word “text”): :%s/text //g 34. Navigate to beginning of file then press i to enter insert mode to add text: Keys 1G i Hello and Function Go to beginning of file (Shift+G) Enter insert mode Add a space after “and” 1G i Hello and Notice that while in insert mode the text “--INSERT --“ appears in the lower-left corner of the screen as shown in the image below: 35. Exit insert mode and return to command mode by pressing the Escape key: Notice that the cursor is now between the words “and” and “Welcome.” 16 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 36. Move forward one space by pressing the lower case l to place the cursor on the ‘W’ and toggle it to lower case by pressing the tilde: Keys l ~ Function Lowercase ‘L’ moves forward one space Shift+` changes letter to lower case 37. Save the file. Press the Escape key to ensure we are in command mode. Then the : followed by then a w and Enter: 38. Press Enter to commit the change. Observe message in lower left indicating the file has been written: 39. Navigate to the space between the word "powerful" and "editor" in the second line as shown in the image below. You could press j followed by 10l or use the arrow keys: Command Function/Keys j Move down to second 10l 10 followed by the lowercase ‘L’ 17 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 40. Add text to the right of the cursor by pressing the letter a. This moves the cursor to the right and enters insert mode. Type the word text followed by a space as shown in image below: Command a text Function/Keys Enter insert mode. text followed by a space 41. Exit insert mode by pressing the Escape key. 42. Open a blank line below the current line: o 43. Enter the following text: This line added by pressing lowercase o. 44. Exit insert mode by pressing the Escape key: 18 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 45. Open a blank line above the current line by pressing uppercase ‘ O’: O 46. Enter the following text: We just pressed O to open a line above. 47. Exit insert mode by pressing the Escape key. 48. Save the file and close the vi editor using any one of the following methods: Command Function/Keys :x :wq :wq! ZZ :q! :e! Will save and close the file. Will write to file and quit. Will write to a read-only file, if possible, and quit. Notice that no colon : is used in this case. Exit without saving changes Discard changes and reload file :w! Write to read-only, if possible. 19 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 49. Once again open myfile using the vi editor: vi myfile 50. Navigate to the third line, delete the third and fourth lines: 3G 2dd 51. Press the Escape key to confirm you are in command mode. 52. Quit the vi editor without saving your changes: :q! If you are not sure that you have made the correct changes or just want to start over, be sure to use :q!. If in doubt, exit without saving (:q!)! 53. Open myfile with the vi editor: vi myfile Notice that lines 3 and 4 are still present. 20 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 54. Search forward for the word line. You’ll notice the cursor moves to the beginning of the first instance of the word line as shown in image below. Enter the following command and press Enter: /line 55. Search for the next instance of the word line by pressing the letter n: n 21 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 56. Search backward for the word line. You’ll notice the cursor moves to the beginning of the previous instance of the word line as shown in image below: ?line 57. Search for the previous instance of the word line by pressing the letter n. Since there are none in this direction, vi will wrap around the document: n 22 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 58. We will replace the word line with the word entry. When we press cw the word line will disappear and we will be in insert mode: cw entry 59. Press Escape key to exit insert mode. 60. Add text at the beginning of a line. Enter insert mode again and add a line by pressing upper case i: I Insert modes include: i, I, a, A, o, and O. Finally press Enter followed by the Escape key. 23 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 61. Add text at the end of a line (uppercase A). First move to the second line and add the phrase Indeed!: 2G A [Space]Indeed! Then press the Escape key. 62. Save your changes and exit vi: :x 63. Create a sample/test file in our home directory for modification with vi: cd cat /etc/passwd > mypasswd vi mypasswd 24 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 64. Replace the first instance of the word root with the word superuser in the current line (the first line in this case): :s/root/superuser/ 25 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 65. Undo the replacement by pressing the letter: u 66. Replace every instance of the word root with the word superuser in the current line (the first line): :s/root/superuser/g 67. Quit without saving: :q! 68. Create another sample/test file for modification with vi. Notice all the stderr (standard error) messages sent to stdout (standard output): find / -name *MESSAGES* > find.out 26 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 69. Run the command again but send the standard error stream to /dev/null. This time no error messages are sent to stdout and thus, not displayed: find / -name *MESSAGES* > find.out 2> /dev/null 70. Open the find.out file using the vi editor: vi find.out 71. Replace every single instance of the word MESSAGES with the word message in the entire file: :%s/MESSAGES/message/g Notice total number of substitutions is reflected in the lower left of the editor: 27 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License. Linux+ Lab Series (LX0-101) Lab 10A: Managing Text Files 72. Save the changes and exit vi by pressing Escape key and then type: wq! 73. Click the X in the upper-right corner to close the terminal window. 74. Close the Fedora Workstation remote pc window. 75. Click the I’m Done button to end the reservation. 28 This work by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License.
0
You can add this document to your study collection(s)
Sign in Available only to authorized usersYou can add this document to your saved list
Sign in Available only to authorized users(For complaints, use another form )