12-03-2017
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.
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
I'm trying to read a bunch of log files and output the lines that contain particular strings.
To accomplish this, I've been running the following from the command line:
find . -name "*" | xargs grep " " | grep " " > output.txt
Two grep statements are needed in case I'm looking for a... (3 Replies)
Discussion started by: Rally_Point
3 Replies
2. Shell Programming and Scripting
i want to search in the current directory all the files that contain one word for example "hello"
i want to achieve it with the grep command but not with the grep * (2 Replies)
Discussion started by: aintour
2 Replies
3. Shell Programming and Scripting
Hello.
I would like to ask your help regarding the cp command. We are using a cp command to create a back-up copy of our file but to no avail. It's just not working. We already checked the file and directory permissions and all seems correct.
We have a script (ftp.script) which calls on... (1 Reply)
Discussion started by: udelalv
1 Replies
4. Shell Programming and Scripting
Hi All,
Is there any way where we can run few commands with different shell in a shell script ?
Let's have an example below,
My first line in script reads below,
#!/bin/sh
However due to some limitation of "/bin/sh" shell I wanted to use "/bin/bash" while executing few... (9 Replies)
Discussion started by: gr8_usk
9 Replies
5. Shell Programming and Scripting
I am running the export command within a view to use that value inside my build script. But while executing it it is saying "export command not found"
My code is as follows:
--------------------------
#!/bin/sh
user="test"
DIR="/bldtmp/"$user
VIEW="test.view1"
echo "TMPDIR before export... (4 Replies)
Discussion started by: dchoudhury
4 Replies
6. Shell Programming and Scripting
Hi All,
I have a question related to Shell scripting. In my shell script, I have following two commands in sequence:
sed 's/^/grep "^120" /g' $ORIGCHARGEDAMTLIST|sed "s;$;| cut -f$FIELD_NO1 -d '|' | awk '{ sum+=\$1} END {printf (\"%0.2f\\\n\", sum/100)}' >$TEMPFILE
mv $TEMPFILE $ORIGFILE... (3 Replies)
Discussion started by: angshuman
3 Replies
7. Post Here to Contact Site Administrators and Moderators
Variable I have in my shell script
diff=$1$2.diff
id=$2
new=new_$diff
echo "My id is $1"
echo "I want to sync for user account $id"
##awk command I am using is as below
cat $diff | awk -F'~' ''$2 == "$id"' {print $0}' > $new
I could see value of $id is not passing to the awk... (0 Replies)
Discussion started by: Ashunayak
0 Replies
8. Shell Programming and Scripting
I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist.
diff=$1$2.diff
id=$2 new=new_$diff
echo "My id is $1"
echo "I want to sync for user account $id"
##awk command I am using is as below
cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies
9. UNIX for Dummies Questions & Answers
I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist.
diff=$1$2.diff
id=$2 new=new_$diff
echo "My id is $1"
echo "I want to sync for user account $id"
##awk command I am using is as below
cat $diff |... (1 Reply)
Discussion started by: Sarita Behera
1 Replies
10. UNIX for Dummies Questions & Answers
Hi everyone
I have a problem with my script
If I try directly this command
/usr/bin/nice -n 19 mysqldump -u root --password="******" wiki_schneider -c | nice -n 19 gzip -9 > /point_de_montage/$(date '+%Y%m%d')-wiki-db.sql.gz
It works
But if I simply add this command in a script and... (8 Replies)
Discussion started by: picemma
8 Replies
LEARN ABOUT DEBIAN
sort-dctrl
SORT-DCTRL(1) Debian user's manual SORT-DCTRL(1)
NAME
sort-dctrl - sort Debian control files
SYNOPSIS
sort-dctrl [options] [ file ... ]
sort-dctrl --copying | --help | --version | -ChV
DESCRIPTION
The sort-dctrl program sorts Debian control files according to specified criteria.
A Debian control (dctrl) file is a semistructured single-table database stored in a machine-parseable text file. Such a database consists
of a set of records; each record is a mapping from field names to field content. Textually, records are separated by empty lines, while
each field is encoded as one or more nonempty lines inside a record. A field starts with its name, followed by a colon, followed by the
field content. The colon must reside on the first line of the field, and the first line must start with no whitespace. Subsequent lines,
in contrast, always start with linear whitespace (one or more space or tab characters).
The sort-dctrl program recognizes two field types: string fields and version fields. Version fields act also as numeric fields. String
fields are compared according to strict lexicographical octet-by-octet comparison, after ignoring any initial whitespace after the colon.
Version fields are parsed and compared as Debian version numbers. When comparing version numbers, if a field content does not in fact con-
form to the version number syntax, it compares less than any field content that does conform, and equal to any other nonconforming field
content. The sort-dctrl program assumes all fields are string fields unless told otherwise.
You can specify arbitrary number of keys for sorting, using the -k option. The keys are interpreted in a descending order of priority: the
first key specified is primary, the second key specified is secondary, and so on. If two records compare equal under the primary key, then
they are compared under the secondary key, and so on. If no keys are specified, a default key is assumed (the "Package" field with no mod-
ifiers).
OPTIONS
-k keyspec, --key-spec=keyspec
Specify one or more keys for sorting. You may specify this option any number of times. The keyspec argument consists of a comma-
separated list of key specifications. Each key specification consists of the name of the field that serves as the key, optionally
followed by a colon and key modifiers. The following key modifiers are supported:
r Invert the comparison for this key, reversing the sorting order.
v Treat this field as a version number field.
n Treat this field as numeric, which currently is synonymous with v.
-q, --quiet, --silent
Output nothing to the standard output stream. Instead, exit immediately after finding the first match.
-l level, --errorlevel=level
Set log level to level. level is one of fatal, important, informational and debug, but the last may not be available, depending on
the compile-time options. These categories are given here in order; every message that is emitted when fatal is in effect, will be
emitted in the important error level, and so on. The default is important.
-V, --version
Print out version information.
-C, --copying
Print out the copyright license. This produces much output; be sure to redirect or pipe it somewhere (such as your favourite
pager).
-h, --help
Print out a help summary.
EXAMPLES
Here are some sample invocations of the program:
sort-dctrl /var/lib/dpkg/available
Output the dpkg available file sorted by the package name.
sort-dctrl -k Version:v /var/lib/dpkg/available
Output the dpkg available file sorted in ascending order of version numbers.
sort-dctrl -k Version:vr /var/lib/dpkg/available
Output the dpkg available file sorted in descending order of version numbers.
sort-dctrl -k Package,Version:v /var/lib/dpkg/available
Output the dpkg available file sorted primarily in ascending order of package names and secondarily in descending order of version
numbers.
sort-dctrl -k Installed-Size:n,Size:nr /var/lib/dpkg/available
Output the dpkg available file sorted primarily in ascending order of installation sizes and secondarily in descending order of
package sizes.
AUTHOR
The program and this manual page were written by Antti-Juhani Kaijanaho <ajk@debian.org>.
SEE ALSO
Debian Policy Manual. Published as the Debian package debian-policy. Also available in the Debian website.
grep-dctrl(1)
Debian Project 2005-06-08 SORT-DCTRL(1)