Shell Script: Sorting by column using grep/awk | Unix Linux Forums | Homework & Coursework Questions

  Go Back    


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


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-15-2013
tburns517 tburns517 is offline
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
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.


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).
Sponsored Links
    #2  
Old 03-15-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 23 October 2014, 6:57 PM EDT
Location: Saskatchewan
Posts: 19,677
Thanks: 822
Thanked 3,351 Times in 3,138 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.
Sponsored Links
    #3  
Old 03-15-2013
DGPickett DGPickett is offline Forum Advisor  
Registered User
 
Join Date: Oct 2010
Last Activity: 23 October 2014, 5:35 PM EDT
Location: Southern NJ, USA (Nord)
Posts: 4,466
Thanks: 8
Thanked 547 Times in 525 Posts
You can substring with decimals, too: -k 1.3,1.9
    #4  
Old 03-15-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 24 October 2014, 5:10 AM EDT
Location: San Jose, CA, USA
Posts: 4,898
Thanks: 182
Thanked 1,645 Times in 1,396 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
Sponsored Links
    #5  
Old 03-16-2013
tburns517 tburns517 is offline
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
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
Sponsored Links
    #6  
Old 03-16-2013
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 24 October 2014, 5:10 AM EDT
Location: San Jose, CA, USA
Posts: 4,898
Thanks: 182
Thanked 1,645 Times in 1,396 Posts
Quote:
Originally Posted by tburns517 View Post
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 ]

Sponsored Links
    #7  
Old 03-16-2013
tburns517 tburns517 is offline
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..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
grep/awk column or variable? Oriksagtaria Shell Programming and Scripting 6 01-30-2011 12:11 AM
Dynamic column finding in awk shell script gvinayagam Shell Programming and Scripting 7 11-19-2010 09:31 AM
AWK/GREP sorting help sam127 Shell Programming and Scripting 8 08-11-2010 10:05 AM
Shell script / Grep / Awk to variable and Loop Spoonless Shell Programming and Scripting 0 01-26-2010 08:11 AM
grep, awk, typeset in a shell script.. nick_25 Shell Programming and Scripting 7 08-07-2008 10:02 AM



All times are GMT -4. The time now is 08:01 AM.