DistributedNetworks DistributedNetworks


Unix Commands  «Prev  Next»

Using Variables with Sort Command - Exercise

The sort command


Objective: Practice using the sort command in unix.

Exercise scoring

This exercise is worth 3 points and is auto-scored.

Background/overview

Access the volleylist file from the course project directory.

Instructions

The volleylist file should be in a subdirectory called course-project under your home directory. You will write three commands to sort the volleylist file:
1. Write a sort command to sort the volleylist file by the player’s last name.
2. Write a sort command to sort the volleylist file by the player’s zip code.
3. Create a variable containing the sort field number
$ col=2.
5. Now write a sort command to sort the volleylist file by this column.

Submitting your exercise

  1. Use the sort command with –t: because a colon is the field separator in the volleylist file. The player’s last name is the third column in the file, so you must use +2 since column numbering begins at zero.
$ sort -t: +2 volleylist 
Ron:Stinky:Baker:10555 Apple St.:Fresno:CA:96888 
Mark:Toothpick:Castile:25 Lemon Ln.:Orange:CA:95001 
June:Chunko:Hartman:325 1st Ave.:Sacramento:CA:96012 
Anna:Spike:Smith:12 Main St.:Palo Alto:CA:94303 
  1. Use the sort command, again using –t: to indicate a colon field separator. Since the zip code is the seventh field in the file, use +6 to sort on this field.
$ sort -t: +6 volleylist Anna:Spike:Smith:12 Main St.:Palo Alto:CA:94303 Mark:Toothpick:Castile:25 Lemon Ln.:Orange:CA:95001 June:Chunko:Hartman:325 1st Ave.:Sacramento:CA:96012 Ron:Stinky:Baker:10555 Apple St.:Fresno:CA:96888
3. Set the col variable to two before you run your sort command. Use $col in place of the field number in the sort command. $ col=2 $ sort -t: +$col volleylist Ron:Stinky:Baker:10555 Apple St.:Fresno:CA:96888 Mark:Toothpick:Castile:25 Lemon Ln.:Orange:CA:95001 June:Chunko:Hartman:325 1st Ave.:Sacramento:CA:96012 Anna:Spike:Smith:12 Main St.:Palo Alto:CA:94303