Home Search Forums Register Forum Rules Man Pages Unix Commands Linux Commands FAQ Members Search Today's Posts Mark Forums Read

 Homework & Coursework Questions Students must use and complete the template provided. If you don't, your post may be deleted! Special homework rules apply here.

# Shell Script: Sorting by column using grep/awk

## Homework & Coursework Questions

#1
03-15-2013
 tburns517 Registered User Join Date: Mar 2013 Last Activity: 16 March 2013, 2:05 PM EDT Posts: 6 Thanks: 0 Thanked 0 Times in 0 Posts
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

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.

4. 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).
#2
03-15-2013
 Corona688   Mead Rotor Join Date: Aug 2005 Last Activity: 21 August 2017, 6:11 PM EDT Location: Saskatchewan Posts: 22,336 Thanks: 1,116 Thanked 4,216 Times in 3,899 Posts
Use the sort command for sorting.

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

Code:
`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.
#3
03-15-2013
 DGPickett   Registered User Join Date: Oct 2010 Last Activity: 1 February 2016, 3:35 PM EST Location: Southern NJ, USA (Nord) Posts: 4,673 Thanks: 8 Thanked 587 Times in 560 Posts
You can substring with decimals, too: -k 1.3,1.9
#4
03-15-2013
 Don Cragun   Administrator Join Date: Jul 2012 Last Activity: 21 August 2017, 5:47 PM EDT Location: San Jose, CA, USA Posts: 10,465 Thanks: 533 Thanked 3,660 Times in 3,123 Posts
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:

Code:
`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
#5
03-16-2013
 tburns517 Registered User Join Date: Mar 2013 Last Activity: 16 March 2013, 2:05 PM EDT Posts: 6 Thanks: 0 Thanked 0 Times in 0 Posts
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

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
#6
03-16-2013
 Don Cragun   Administrator Join Date: Jul 2012 Last Activity: 21 August 2017, 5:47 PM EDT Location: San Jose, CA, USA Posts: 10,465 Thanks: 533 Thanked 3,660 Times in 3,123 Posts
Quote:
 Originally Posted by tburns517 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:

Code:
`if test "\$choice" some condition check`

or

Code:
`if [ "\$choice" some condition check ]`

#7
03-16-2013
 tburns517 Registered User Join Date: Mar 2013 Last Activity: 16 March 2013, 2:05 PM EDT Posts: 6 Thanks: 0 Thanked 0 Times in 0 Posts
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

Last edited by tburns517; 03-16-2013 at 12:52 PM..

 More UNIX and Linux Forum Topics You Might Find Helpful Thread Thread Starter Forum Replies Last Post Oriksagtaria Shell Programming and Scripting 6 01-30-2011 12:11 AM sam127 Shell Programming and Scripting 8 08-11-2010 10:05 AM Spoonless Shell Programming and Scripting 0 01-26-2010 08:11 AM nick_25 Shell Programming and Scripting 7 08-07-2008 10:02 AM

All times are GMT -4. The time now is 07:34 PM.