Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Before you begin Before answering the questions, explore the directories and exp

ID: 667795 • Letter: B

Question

Before you begin

Before answering the questions, explore the directories and experiment with Unix commands. In particular, familiarize yourself with the commands:

cd ls cat head tail cut sort uniq tr wc find

Questions

Each of these questions should be expressible as a single command or pipeline of commands that run from your assignment 2 sub-dir. You should not need multiple lines or semicolons for any question.

Part 1 (1 point each)

Using find, write commands that search for files in the following directory: /usr/local/tmp/election2008

1. List all .csv files.
2. List .csv files in the sub-dir Aug.
3. List all files from the first 9 days of August.
4. List all files from the first 9 days of July and August. 5. List only .csv files from before August 10.

Part 2 (5 points)

Using head and tail, write a command to extract the second section of a file (i.e. the data section). If you are really confused, this info is bullet #2 from the previous page.

Turn this into an executable script called extractdata (you do not need to hand this in). Then, using find and extractdata, write a command to get the second section of all .csv files in the month directories, and place the output into a file called polls.csv. Be sure to keep this file in your homedir. You will use it again on the next assignment.

Hint: Inside the script don’t forget the command line variable $1. Example: head -52 $1

I am just confused on Part 2

Explanation / Answer

The basics re executable scripts is as follows -:
   #!/bin/sh as the first line.
   chmod +x to make the script executable.

   So, $1 is the first parameter passed to the script. Therefore a script called "demo" that scrambles a file, "demo somefile" is written in order to scramble somefile,so inside the script, whatever's done to $1 is going to be done to somefile.

So, "tail" outputs the last few lines (or however much that is told by programmer to output) of a file, and "head" outputs the first few; therefore "the second section of all .csv files", can be grabbed using head and tail. For example, if "the second section" is the lines 11-20, following is done :
       head -n 20 | tail -n 10
   It gets the first 20 lines (1-20), then passes these to "tail" which grabs the last ten (11-20)? Using a combination of head and tail enables to grab sections of files.

Continuing up, the bit about selection a particular date range of files depends on other things, including the immediate context (e.g. what's in the files). We can use the "find" command to list files modified at certain times/dates, but we have files that are *named* according to dates/times (subdirectories called "August", for example) so it's easy enough just to work with ls.

Remember that the output of one Unix-like command can be piped as input for another one (that's the beauty of Unix). So if we can use ls -t to sort files according to time, we can do the same kind of thing as described earlier with head and tail (e.g. if we're in a directory with a file for each day of a month but the files have random names, we can ls -t | head -n 5 to list the five most recent, or ls -t | tail -n 5 to list the five earliest).

Don't forget to use the --help switch with Unix commands (or type man whatever-command) to browse through the things we can do with them.
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote