Homework 3 -- due Oct. 9

02 Oct 2011

For this homework assignment, you'll be creating several text files by using command line utilities. In addition, create another text file called csc110-hw3.txt to contain answers to other questions.

See this post for a summary of the command line concepts we covered in class, as well as links to more information and reference sites.

To submit your homework, create a zip file containing all the files you're asked to create, and email to jal2016@email.vccs.edu with subject "CSC 110 hw3".

On Windows, you can create a zip file by moving all of your files to a folder, and then right-clicking on the folder and choosing "Send to / zip file".

  1. The /b flag causes the dir command to list just filenames (no other information). Create a text file that contains the contents of C:\Windows\System32. Create a second text file that contains the same files, except listed in reverse alphabetical order, by piping the output of your first command through a call to sort /r (the sort command sorts its input, and the /r flag specifies that it should be sorted in reverse order).

  2. Browse this command line reference site, and select three commands you'd like to try out. (Read the descriptions to make sure that they don't do anything destructive to your comptuer). You can also type help at the command line for a summary of some of the available commands. For each command, execute the command and redirect the output to a file called command-name.txt (except replace command-name with the actual name of the command you choose).

    For example, if I chose the dir command, I would execute dir > dir.txt. You should choose commands we have not already used in class.

    Submit the two files you create as part of your homework zip file. In addition, in your homework answers file, briefly describe each command and explain why they're useful.

  3. Run the following commands:

    set a=1
    set b=2
    set ab=%a% + %b%
    set /a c=%a% + %b%
    • After running those commands, what is the output of the following commands?

      echo c = %c%
      echo ab = %ab%
    • What are the commands you'd run to execute the following?

      • Create a variable named x with value 1234.
      • Create a variable named y and set it to the value of x squared.
      • Print the value of y.
  4. Download this copy of The Adventures of Huckleberry Finn from Project Gutenberg.

    This exercise will use the find command to answer some questions about the book's contents. The command find "ABC" FILENAME prints all lines in FILENAME that contain the letters "ABC". The command find "ABC" without a filename prints all the lines from standard input that contain "ABC". There are some useful flags for the find command. /i makes the search case-insensitive (so the above would match both "ABC" and "abc" or "aBC", etc.). /c causes the command to print only a count of the lines it found.

    • How many lines contain "Huck"?
    • How many lines contain "Jim"?
    • Create a text file that contains all the lines that contain "Jim".
    • How many lines contain both "Huck" and "Jim"? Hint: Use one find command to find all the "Huck" lines, and pipe the output to another find command that finds the "Jim" lines.

    Sometimes we want to search something more structured than just a few letters. For example, we might want to match a full word, or find words that start with a given letter. The findstr command can accomplish this. It allows special formatting in the target argument that allows those kind of patterns.

    Here are some examples:

    • findstr "\<A" finds all lines with a word that starts with "A"
    • findstr "b\>" finds all lines with a word that ends with "b"
    • findstr "abc.e" finds all lines that contain "abc?e", where "?" can be any single character.
    • findstr "abc[a-z]*e" finds all lines that contain "abc?e", where "?" can consist of one or more letter from "a" to "z".

    Using the findstr command, do the following:

    • Create a text file that contains all the lines that have a word that starts with "q". How can you combine this with a call to find /c that will count up all such lines for you?
    • Create a text file that contains all the lines that have a word that starts with "a" and ends with "b".