Shell script help not sure of next command

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

I am using GNU/Linux -bash shell

I am to Create a shell script named top_veg_cost to display the top 2 most costly vegatables for a class lab assignment

My shell script that I have so far is

top_veg_cost=$(cat -n Thanksgiving/Loaded_Mashed_Potatos/Ingredients.csv Thanksgiving/Meat_Loaf/Ingredients.csv Thanksgiving/Pumpkin_Pie/Ingredients.csv | grep vegatable)

echo "$top_veg_cost"

out come I have so far is

     1  Potato,vegatable,0.89,5
     4  green_onion,vegatable,0.99,3
    11  onion,vegatable,0.89,2
    12  bell_pepper,vegatable,0.89,2
    21  pumpkin_pie_filling,vegatable,2.98,1

when I add a sort command to the script like this

top_veg_cost=$(cat -n Thanksgiving/Loaded_Mashed_Potatos/Ingredients.csv Thanksgiving/Meat_Loaf/Ingredients.csv Thanksgiving/Pumpkin_Pie/Ingredients.csv | grep vegatable | sort -t, +2 -3n)

echo "$top_veg_cost"

new out come is

    11  onion,vegatable,0.89,2
    12  bell_pepper,vegatable,0.89,2
     1  Potato,vegatable,0.89,5
     4  green_onion,vegatable,0.99,3
    21  pumpkin_pie_filling,vegatable,2.98,1

I have no idea what command I should add to get the 2 highest cost vegetables which are

 21  pumpkin_pie_filling,vegatable,2.98, 
  4  green_onion,vegatable,0.99,3

I might even need to change what I have just not sure

going to need more help with other sections of same lab just have not reached them yet

Waubonsee Community College
Sugar Grove Illinois
Professor: Tim Lippold
Linux/UNIX Operating System CIS180
Book being used is Harley Hahn's Guide to Unix and Linux

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

Assuming that the last field in your data is a number of units of that recipe item, are you looking for the highest unit price (e.g. $2.98 for pumpkin pie filling), or are you looking at the highest price based on the unit price and quantity (e.g., $4.45 for 5 potatoes)?

If the latter, it is unfortunate that grep doesn't do arithmetic and that bash doesn't perform floating point calculations. Are you supposed to be learning how to use awk ?

Most versions of the sort utility since about 1990 have two ways of specifying which ranges of characters on an input line are to be used as a sort key. Many people (apparently including you) found the old way ( +key_start_spec -key_end_spec ) confusing. Please look at your system's man page for sort (i.e. issue the command man sort ) and look for the -k keydef option description and see if you can more easily specify the unit price field to be used as the sort key. You might also want to look for an option (or keydef flag to reverse the sort order if you want the highest values first instead of last. Note that if you want to sort on two fields, you need to sort keys. For example, if you want to sort with unit prices as the primary key and quantity as the secondary key, you need to use the 3rd field as your primary key and the 4th field as your secondary key. Using the 3rd and 4th fields together as a single sort key won't give you the results you want when sorting numeric fields.

While you're looking at man pages, you might also want to investigate what the head and tail utilities do.

Further you need to set a suitable field delimiter.
man sort is your friend.

The sort command used in post #1 in this thread was:

sort -t, +2 -3n

Isn't <comma> a suitable field separator for the sample data shown (as long as the ingredient name is not a sort key field)?

Sorry, I didn't notice the -t, there :o

Regarding the -k description, it is a bit lengthy in the man pages, and it is not even detailed enough.
For example, I had to find out the hard way that a global -n option does not work well with multiple key fields, but giving each -k a postfix n works. I even found that -k 1,1 is often better than -k 1 . But never found out if 1,1n is equivalent with 1n,1 or 1n,1n ... :frowning:

Hi MadeInGermany,
The sort man page description of sort keys (whether using -k keydef or +key_start -key_end ) and of the options/flags is complicated because there are so many special cases in the way sort keys are specified. If you look at lines as C arrays of type char where the index of the first character in the array is 0, the old way of specifying key definitions will feel "right" to you. If you look at the way most other UNIX utilities number fields and characters within fields (e.g., awk , cut , fold , head , paste , and tail ), the -k keydef key definitions will feel "right" to you. I can translate between the two, but I generally find I'm more likely to get the new form correct the first time. And, I find it much easier to explain the new form to a UNIX utility newby whether or not they are experienced C programmers. Most of the people that I know who feel more comfortable with the old form learned to use the old form before the late 1980's when the new form was invented.

The options -b , -d , -f , -i , -n and -r can be given as options that apply to all sort keys that do not include any flags or as flags that only apply to the key to which they are attached. The -t char option can only be specified as an option; not as a flag.

When used as a flag, b applies only to the start field or end field specification to which it is attached. All other flags can be applied to the start field specification, to the end field specification, or both and have the same effect.

If a key field definition has any flags attached, those flags override ALL options except -t char . So, for example, if I want to sort in reverse (decreasing) numeric order on unit price and on reverse case-insensitive alphabetic order by ingredient name within groups of identical unit prices I could use any of the following:

sort -t, -k3r,3n -k1.9f,1r file
sort -t, -k3nr,3rn -k1.9fr,1rf file
sort -frt, -k3rn,3 -k1.9,1 file
sort -nrt, -k3,3 -k1.9,1fr file

but the following won't work:

sort -rft, -k3,3n -k1.9,1 file

because the n flag on the first key specification overrides both the -r and -f options.

The main difference between the two forms is that field numbers and characters in fields are numbered from 1 when using -k keydef , but are numbered from 0 when using +key_start -key_end . For example, to sort alphabetically on the 7th character of the first field skipping over leading spaces in the sample data shown in post #1, the following four commands all specify the same sort key:

sort -t, -k1.7b,1.7b file
sort -t, +0.6b -0.6b file
sort -bt, -k1.7,1.7 file
sort -bt, +0.6 -0.6 file

and all produce the output (note that there are two <space>s between the line number and the ingredient name in the 1st field):

     1  Potato,vegatable,0.89,5
     4  green_onion,vegatable,0.99,3
    11  onion,vegatable,0.89,2
    12  bell_pepper,vegatable,0.89,2
    21  pumpkin_pie_filling,vegatable,2.98,1

In the most general -k keydef option form:

-k field_start_number[.first_character_number][flag...][,field_end_number[.last_character_number][flag...]]

if the optional .first_character_number is omitted, it defaults to the first character in the given field. If the optional .last_character_number is omitted, it defaults to the last character in the given field. If the entire optional end field number specification (,field_end_number[.last_character_number][flag...] ) is omitted, it defaults to the end of the current line.

An equivalent sort key using the old style key definition for the above -k option is:

+field_start_number-1[.first_character_number-1][flag...] [-field_end_number-1[.last_character_number-1][flag...]]

where all four occurrences of -1 are numeric calculations on the previous number; not literal strings.

I hope this helps. :cool: And, I really hope I don't have any typos in this post that further confuse any readers trying to figure out how sort works. :eek:

I guess no typo outside the odd vegatable, but that's off topic... :wink:

Yeah. I noticed it too. I was just reporting results from using a sort command on the data provided in post #1. Classic GIGO.

You certainly meant MIG not RudiC.
Anyway, a big thanks for the light on this dark area!

Just trying to get the highest costing vegetables does not have any thing to do with quantities

I have not learned awk yet

---------- Post updated at 11:33 AM ---------- Previous update was at 10:41 AM ----------

please simple answers I am having hard time under standing Linux UNIX lingo

I want an out come similar to

    21  pumpkin_pie_filling,vegatable,2.98,1
     4  green_onion,vegatable,0.99,3

With everything we have shown you about using sort , have you figured out how to do a reverse numeric sort on the unit price field?

Did you look at the manual pages for head and tail on your system?

1 Like