Extra Credit 8 - Text Files 1
Before we start!
Run the following command to generate the files we will be working with. This is the same command that you will find on page 3 of the presentation: “Handling Text Files Part 1”
cd ~/Documents/ && git clone https://github.com/linuxworkshop67/sample_files
What will you learn
- Learn how to display and inspect text files using
cat,tac,head, andtail - Practice extracting specific information from files using the
cutcommand - Learn how to sort text data using the
sortcommand - Develop the ability to work with structured text files such as CSV files
- Practice combining commands and analyzing text data from the Linux terminal
CAT & TAC
These two commands display the contents of an entire file in the terminal. These commands display the contents of a file directly in the terminal without modifying the file. When should you use these commands? Use these commands whenever you want to display the complete contents of a file.
- Navigate to the
~/Documents/sample_files/Markdown/directory and display the content of the following files:blog_post.md
resume.md - Repeat the previous command with line numbers.
- Display the content of the same files but in reverse order
Take a screenshot of your terminal to submit later.
HEAD & TAIL
We do not always want to see the entire file in the terminal. There are times when a file is too long and we just want a specific number of lines to be displayed. This is where head and tail come in handy. head displays a specified number of lines starting from the beginning of a file, whereas tail displays a specified number of lines starting from the end of a file. These commands are useful when you only need a specific section of a file. Sometimes we only need specific information from a text file.
- Navigate to the directory:
~/Documents/sample_files/Code, where you will find Hello World written in different programming languages. Display the first line of each file using a wildcard. (hello*) - Display the first 10 lines of the file
helloWorld.vala - Display the last line of the Bash and Python script
- Display the last 10 lines of the file
helloWorld.c
Take a screenshot of your terminal to submit later.
CUT
NOTE:
Thecutcommand commonly uses-dto specify a delimiter and-fto specify the field or column to display.
Sometimes we want specific information inside a text file. In these times, we need to use utilities that will allow us to process the file line by line and run a specific action on each line. cut is a utility that extracts specific sections of text from each line of a file. from a file line by line. By the way, this does not affect the file. These commands process and display data without modifying the original file, which means that the file never gets modified. We will learn how to save the output later.
- Navigate to the directory
~/Documents/sample_files/Csv. There you will find a bunch of csv files (comma-separated values). - Use the
headcommand to display the first line of the filecars.csv. This will allow you to know the headers of each column and the delimiter used in the csv file. - Use the cut command to generate a list of cars.
- Use the cut command to generate a list of cars, including the number of cylinders and country of origin
Take a screenshot of your terminal to submit later.
SORT
The sort command is used to arrange lines of text in a specific order. It is useful when we want to sort data. Review the presentation to understand how sort determines ordering.
- Navigate to the directory
~/Documents/sample_files/Csv. There you will find a bunch of csv files (comma-separated values). - Use the file contents to identify the delimiter and determine which column contains the information you want to sort by.
- Sort the
cereal.csvfile using the first column as the sorting key. - Repeat the previous command and save the output to a file named
sorted_cereals.csv
Take a screenshot of your terminal to submit later.
What will you submit for extra credit
If this is your first extra credit assignment, you will need to create the necessary parent directory structure. In your
cis106directory (local repository), create a new directory calledExtraCredit(Use one word with no spaces in directory names.)
- Inside
ExtraCredit, create another directory calledextra8 - Open your
cis106local repository in VS Code - Inside
extra8, create a Markdown file calledextra8.md - Add all the screenshots you took earlier to
extra8.mdusing proper Markdown syntax and label each screenshot using Heading 2 Markdown headings. Save the Markdown file. - Your Markdown document should start with the following Heading 1:
# Extra Credit 8: Handling Text Files 1 - Convert your file to PDF
- Run the following GitHub commands in the VS Code terminal, to push all your changes to GitHub:
git add .git commit -m "Completed Extra 8"git push
- In Blackboard, submit the following:
- The URL of
extra8.md - The PDF file
extra8.pdf
- The URL of