Shell Script: Sorting by column using grep/awk

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    You will write a script that will read a tab-separated file that contains the names of all 50 states & Washington DC, their populations, their land areas and their population densities, which is used to order the current list. The script will ask the user if they would like to have a new file sorted alphabetically by state name, by land area or by population. If they select by name or population, the file will output the state name, then the population, then the land area. If they select by land area, the file will output the state name, then the land area, then the population. The script will save the new file either as �states_alpha.txt�, �states_land.txt� or �states_pop.txt�, depending on the choice selected by the user.

  2. Relevant commands, code, scripts, algorithms:
    grep, awk

  3. The attempts at a solution (include all code and scripts):
    echo "How would you like the fifty_states.txt file to be sorted?
    1 ) Alphabetically
    2 ) By Land Area
    3 ) By Population
    Enter your choice: " choice

if $choice=1
grep '^[A-Z]' fifty_states.txt | awk '{ print $1, $2, $3 }' fifty_states.txt > states_alpha.txt

This does not work...I am not sure where to go from here.

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Jackson Community College, Jackson MI, USA, M. Brinkman, CIS106

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Use the sort command for sorting.

To sort a file alphabetically on the first column in ascending order:

sort -k 1,1 < inputfile > outputfile

-k works like -k startcolumn,endcolumn which is why 1 is given twice. It's more complex than this but this is its basic usage.

Add the -r switch to sort in descending order.

Add the -n switch to sort numbers.

See man sort for details.

You can substring with decimals, too: -k 1.3,1.9

tburns517,
In addition to what Corona688 has already said, you also need to note that the echo command just copies strings to its output; it doesn't read a response from its input. To get input from the user, you'll probably want to use the read utility. Note that there is probably a section 1 man page for the read utility and a section 2 man page for the read() function (or system call) on your system. Try:

man 1 read

to get the read utility's man page.

You'll also need to check the syntax of the if command and the test (or [ ) command for the shell you're using. You should be able to find details on the shell's man page. Presumably for a class like this, your shell would be sh, ksh, or bash. By now, you can guess how to find your shell's man page.

  • Don

I need help really quick, this is due in an hour. Here is my new code, I just need to figure out how to get the user input to do the correct command, which is why I left the "if choice" blank...or should it be "if $choice" ?

read -p "How would you like the file to be sorted?
1) Alphabetically by name
2) By Land Area
3) By Population
Enter number choice here: " choice
read choice

if choice
then
awk '{ print $2, $3, $4 }' fifty_states.txt > states_alpha.txt
sort states_alpha.txt
fi

if choice
then
awk '{ print $2, $3, $4 }' fifty_states.txt > states_land.txt
sort -k +1 states_land.txt
fi

if choice
then
awk '{ print $2, $4, $3 }' fifty_states.txt > states_pop.txt
sort -k +1 states_pop.txt
fi

We are not going to do your homework for you.
I suggest again that you look at the man page for the test command so you can craft your if statements to look something like:

if test "$choice" some condition check

or

if [ "$choice" some condition check ]

Do you see an issue with this? I got it to work without errors, but I am not getting an output at all, as in, the file is not being created.

if [ "$choice" = "1" ]
then
awk -F"\t" '{ print $2, $3, $4 }' fifty_states.txt | sort > states_alpha.txt
fi

Note that I added code tags...

I don't see why states_alpha.txt would not be created by this command (assuming that you just entered 1 and a carriage-return after the prompt printed by echo). But I don't see why you are discarding the state's name (the 1st field in your input file) from the data you are feeding into sort.

Try adding:

set -xv

as a new line before the echo in your script so you can see how your shell is expanding the variables it is processing.