Write a bash program to calculate quintiles from a CSV (comma separated value) file.
Question Description
Bash Program
Write a bash
program to calculate quintiles from a CSV (comma separated value) file.
- Read the input lines:
- the 1st and 2nd fields may contain text or integers
- the 3rd (last) field will contain only integers
- calculate the quintiles from lines where the 3rd field is present
- the 1st quintile is the set of numbers smaller than 80% of all the numbers
- the 2nd quintile is the set of numbers larger than 20% and smaller than 60% of all the numbers
- the 3rd quintile is the set of numbers larger than 40% and smaller than 40% of all the numbers
- the 4th quintile is the set of numbers larger than 60% and smaller than 20% of all the numbers
- the 5th quintile is the set of numbers larger than 80% of all the numbers
- For each input line output the following:
- print the 1st, 2nd, and 3rd fields with no changes
- if the 3rd field is present then print the quintile
- separate output fields with commas
- Example usage:
cat input-file | bash script.bash
- Example input lines:
Title
a,b,6
c,d,2
e,f,15
g,h,90
i,j,9
k,l,1
m,n,4
o,p,30
q,r,1
foo,bar,3
- Example output lines:
Title
a,b,6,3
c,d,2,2
e,f,15,4
g,h,90,5
i,j,9,4
k,l,1,1
m,n,4,3
o,p,30,5
q,r,1,1
foo,bar,3,2
Approximation
- If the number of lines with 3rd field present is not divisible by 5:
- the number of lines in the five quintiles will differ but that difference should not be more than 1
- e.g. for 42 lines 8,8,9,8,9 is OK but 8,8,8,8,10 is not
Requirements
- Use only
bash
and standard UNIX utilities: - do not use
awk
or other UNIX programming languages - Read from standard input, write to standard output:
- do not use disk files for input or output
- do not use command line parameters
- output only processed input lines – no prompts, instructions, etc.
Documentation
- Comments
- include name, date, assignment
- include brief instructions for using the program
- include brief explanations of algorithms
- include brief explanations of data structures
- Use descriptive names for variables and functions.
- Use a consistent style to indent code blocks.
Due Date
- This assignment is due May 07 at 11:59 PM.
- Email your program (where script.bash is the name of your file) with this command:
mail -s 'assignment' stuart < <em>script.bash</em>
Hints
- Start early, this may take longer than you expect.
- Think about the structure of your data and logic before you write any code.
- With each revision add the smallest possible amount of code:
- test your code with a variety of inputs
- add comments as necessary
- save your code with another name
-
Q&A on Assignment
- Can I use the
bc
utility in Assignment ? - No, it’s a programming language. The man page says
bc
“is a language that supports arbitrary precision numbers with interactive execution of statements.”
- Can I use the
Have a similar assignment? "Place an order for your assignment and have exceptional work written by our team of experts, guaranteeing you A results."