Homework 3 (MS Windows command line) solutions

25 Oct 2011
  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).

    Answers:

    These commands save the original and reverse orders to files:

    dir /b C:\Windows\System32 > original.txt
    dir /b C:\Windows\System32 | sort /r > reverse.txt
    
  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%
      

      Answers:

      c = 3
      ab = a + b
      
    • 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.

      Answers:

      set x=1234
      set /a y=x*x
      echo %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"?

      Answer: find /c "Huck" prints 75.

    • How many lines contain "Jim"?

      Answer: find /c "Jim" prints 363.

    • Create a text file that contains all the lines that contain "Jim".

      Answer: The command find "Jim" pg76.txt > Jim-lines.txt saves the lines to the file Jim-lines.txt.

    • 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.

      Answer: The command find "Huck" pg76.txt | find /c "Jim" first finds all the "Huck" lines, and then finds which ones also contain "Jim".
      The result is 1.

    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?

      Answer: findstr "\<q" pg76.txt > q-lines.txt To just count the lines, use: findstr "\<q" pg76.txt | find /c "q", which prints 121. Searching for "q" works for the second find call, since we know that every line that results from the first call must contain a "q".

    • Create a text file that contains all the lines that have a word that starts with "a" and ends with "b".

      Answer: findstr "\<a[a-z]*b\>" pg76.txt > a-b-lines.txt saves all the lines with a word that starts with a, ends with b, and has regular letters in between. To handle more complicated words like contractions and hyphenated words, we could add more letters in between the [], but we'd have to be more specific about which words we want to match.