Editing Files Using Substrings - Qwiklabs
Editing Files Using Substrings - Qwiklabs
Introduction
In this lab, you'll change the username of your coworker Jane Doe from "jane" to
"jdoe" in compliance with company's naming policy. The username change has
already been done. However, some les that were named with Jane's previous
username "jane" haven't been updated yet. To help with this, you'll write a bash script
and a Python script that will take care of the necessary rename operations.
What you'll do
Practice using the cat, grep, and cut commands for le operations
Use > and >> commands to redirect I/O stream
Replace a substring using Python
Run bash commands in Python
You'll need to start the lab before you can access the materials in the virtual machine
OS. To do this, click the green “Start Lab” button at the top of the screen.
Editing Files using Substrings
Note: For this lab you are going to access the Linux VM through your local SSH
Client, and not use the Google Console (Open GCP Console button is not available
for this lab).
After you click the “Start Lab” button, you will see all the SSH connection details on
the left-hand side of your screen. You should have a screen that looks like this:
Please nd one of the three relevant options below based on your device's operating
system.
Note: Working with Qwiklabs may be similar to the work you'd perform as an IT
Support Specialist; you'll be interfacing with a cutting-edge technology that requires
multiple steps to access, and perhaps healthy doses of patience and persistence(!).
You'll also be using SSH to enter the labs -- a critical skill in IT Support that you’ll be
able to practice through the labs.
You can download the VM’s private key le in the PuTTY-compatible PPK format
from the Qwiklabs Start Lab page. Click on Download PPK.
Note: Replace username and external_ip_address with values provided in the lab.
Editing Files using Substrings
5. In the Private key le for authentication box, browse to the PPK le that you
downloaded and double-click it.
Note: PPK file is to be imported into PuTTY tool using the Browse option available in
it. It should not be opened directly but only to be used in PuTTY.
Editing Files using Substrings
7. Click Yes when prompted to allow a rst connection to this remote SSH server.
Because you are using a key pair for authentication, you will not be prompted for a
password.
Common issues
You downloaded the fresh new PPK le for this lab from Qwiklabs.
You can download the private key le in PEM format from the Qwiklabs Start Lab
page. Click on Download PEM.
Editing Files using Substrings
To open terminal in Mac (OSX) enter cmd + space and search for terminal.
Note: Substitute the path/filename for the PEM file you downloaded, username and
External IP Address.
You will most likely nd the PEM le in Downloads. If you have not changed the
download settings of your system, then the path of the PEM key will be
~/Downloads/qwikLABS-XXXXX.pem
Note: Make sure you are not in Incognito/Private mode while launching the
application.
You can download the private key le in PEM format from the Qwiklabs Start Lab
page. Click on Download PEM.
Connect to your VM
3. In the username section, enter the username given in the Connection Details Panel of
the lab. And for the hostname section, enter the external IP of your VM instance that
is mentioned in the Connection Details Panel of the lab.
Editing Files using Substrings
4. In the Identity section, import the downloaded PEM key by clicking on the Import…
button beside the eld. Choose your PEM key and click on the OPEN button.
Note: If the key is still not available after importing it, refresh the application, and
select it from the Identity drop-down menu.
5. Once your key is uploaded, click on the [ENTER] Connect button below.
cat
grep
cut
cat:
The cat command allows us to create single or multiple les, view the contents of a
le, concatenate les, and redirect output in terminal or other les.
Syntax:
cat [file]
grep:
The grep command, which stands for "global regular expression print", processes
text line-by-line and prints any lines that match a speci ed pattern.
Syntax:
Syntax:
cut:
The cut command extracts a given number of characters or columns from a le. A
delimiter is a character or set of characters that separate text strings.
Editing Files using Substrings
Syntax:
cut [options] [file]
For delimiter separated elds, the - d option is used. The -f option speci es the eld,
a set of elds, or a range of elds to be extracted.
Syntax:
Redirection into a le
Each stream uses redirection commands. A single greater than sign (>) or a double
greater than sign (>>) can be used to redirect standard output. If the target le
doesn't exist, a new le with the same name will be created.
Commands with a single greater than sign (>) overwrite existing le content.
Commands with a double greater than sign (>>) do not overwrite the existing le
content, but it will append to it.
So, rather than creating a le, the >> command is used to append a word or string to
the existing le.
Exercise
Editing Files using Substrings
The Scenario
Your coworker Jane Doe currently has the username "jane" but she needs to it to
"jdoe" to comply with your company's naming policy. This username change has
already been done. However, some les that were named with Jane's previous
username "jane" haven't been updated. For example, "jane_pro le_07272018.doc"
needs to be updated to "jdoe_pro le_07272018.doc".
cd data
You can list the contents of the directory using the ls command. This directory
contains a le named list.txt. You will also nd some other les within this directory.
cat list.txt
Output:
This le contains three columns: line number, username, and full path to the le.
You could also view the complete /data directory using the ls command.
ls
Editing Files using Substrings
Let's try out the commands we learned in the previous section to catch all the "jane"
lines.
This returns all the les with the pattern "jane". It also matches the le that has string
"janez" within it.
Now, we'll list only the les containing the string "jane" and not include "janez".
Next, we'll use the cut command with grep command. For cut command, we'll use
the whitespace character (‘ ‘) as a delimiter (denoted by -d ) since the text strings
are separated by spaces within the list.txt le. We'll also fetch results by specifying
the elds using -f option.
Output:
Editing Files using Substrings
grep " jane " ../data/list.txt | cut -d ' ' -f 2
Output:
Output:
Test command
We'll now use the test command to test for the presence of a le. The command test
is a command-line utility on Unix-like operating systems that evaluates conditional
expressions.
test EXPRESSION
Editing Files using Substrings
We'll use this command to check if a particular le is present in the le system. We
as a parameter and returns
do this by using the -e ag. This ag takes a lename
True if the le exists.
Output:
We'll now use the redirection operator (>) to create an empty le simply by specifying
the le name. The syntax for this is:
> [file-name]
> test.txt
Output:
To append any string to the test.txt le, you can use another redirection operator
(>>).
Output:
Iteration
For: A for loop repeats the execution of a group of statements over a set of items.
While: A while loop executes a set of instructions as long as the control condition
remains true.
Until: An until loop executes a set of instructions as long as the control condition
remains false.
Let's now iterate over a set of items and print those items.
Output:
This script should catch all "jane" lines and store them in another text le called
oldFiles.txt. You will complete the script using the command we practiced in earlier
sections. Don't worry, we'll guide you throughout the whole process.
cd ~/scripts
nano findJane.sh
#!/bin/bash
Create the text le oldFiles.txt and make sure it's empty. This oldFiles.txt le should
save les with username "jane".
> oldFiles.txt
Now, search for all lines that contain the name " jane " and save the le names into a
variable. Let's call this variable les, we will refer to it with that name later in the lab.
Since none of the les present in the le list.txt are available in the le system, check
if le names present in les variable are actually present in the le system. To do
this, we'll use the test command that we practiced in the previous section.
Now, iterate over the les variable and add a test expression within the loop. If the
item within the les variable passes the test, add/append it to the le oldFiles.txt.
Once you have completed writing the bash script, save the le by clicking Ctrl-o, Enter
key, and Ctrl-x.
chmod +x findJane.sh
Editing Files using Substrings
Run the bash script ndJane.sh.
./findJane.sh
This will generate a new le named oldFiles.txt, which consists of all the les
containing the name "jane".
Use the cat command followed by the le name to view the contents of the newly
generated le.
cat oldFiles.txt
Output:
Check my progress
In this section, you are going to write a Python script, changeJane.py, that takes
oldFiles.txt as a command line argument and then renames les with the new
username "jdoe". You will be completing the script, but we will guide throughout the
section.
Create a Python script changeJane.py under /scripts directory using nano editor.
nano changeJane.py
Editing Files using Substrings
Add the shebang line.
#!/usr/bin/env python3
Now, import the necessary Python module to use in the Python script.
import sys
import subprocess
Since oldFiles.txt is passed as a command line argument, it's stored in the variable
sys.argv[1]. Open the le from the rst argument to read its contents using open()
method. You can either assign it to a variable or use a with block. Hint: traverse each
line in the le using readlines() method. Use line.strip() to remove any whitespaces or
newlines and fetch the old name.
Once you have the old name, use replace() function to replace "jane" with "jdoe". This
method replaces occurrences of any older substring with the new substring. The old
and new substrings are passed as parameters to the function. Therefore, it returns a
string where all occurrences of the old substring is replaced with the new substring.
Syntax:
string.replace(old_substring, new_substring)
Now, invoke a subprocess by calling run() function. This function takes arguments
used to launch the process. These arguments may be a list or a string.
In this case, you should pass a list consisting of the command to be executed,
followed by arguments to the command.
Use the mv command to rename the les in the le system. This command moves a
le or directory. It takes in source le/directory and destination le/directory as
Editing Files using Substrings
parameters. We'll move the le with old name to the same directory but with a new
name.
Syntax:
mv source destination
Now it must be clear. You should pass a list consisting of the mv command, followed
by the variable storing the old name and new name respectively to the run() function
within the subprocess module.
f.close()
chmod +x changeJane.py
Run the script and pass the le oldFiles.txt as a command line argument.
./changeJane.py oldFiles.txt
Navigate to the /data directory and use the ls command to view renamed les.
cd ~/data
ls
Congratulations!
Congrats! You've successfully renamed les containing named "jane" with "jdoe" and
we bet you're feeling much more comfortable performing operations using cat, grep,
cut, and I/O stream commands. On top of that, you now know how to use conditional
statements and iteration through bash scripts. Working with bash and Python
scripting simultaneously will be super helpful throughout your IT specialist career.
Great job completing this lab!
When you have completed your lab, click End Lab. Qwiklabs removes the resources
you’ve used and cleans the account for you.
You will be given an opportunity to rate the lab experience. Select the applicable
number of stars, type a comment, and then click Submit.
You can close the dialog box if you don't want to provide feedback.