0

I have a file laid out like this

gender,yearsExperience,yearsSchool,wage
female,9,13,6.3152956461
female,12,12,5.4797699786
female,11,11,3.6421699174
female,9,14,4.5933365997
female,8,14,2.4181574607

I am trying to find the difference between the minimum wage earned by high school and college grads (12/16 yearsSchool). I have a shell script written as follows:

#High School Min
HighSchoolMin=$(cat wages.csv | cut -d "," -f 3,4 | egrep "^[1]{1}[2]{1}," | tr , " " | sort -k2,2r | tail -n 1 | cut -d " " -f 2)

#College Min
CollegeMin=$(cat wages.csv | cut -d "," -f 3,4 | egrep "^[1]{1}[6]{1}," | tr , " " | sort -k2,2r | tail -n 1 | cut -d " " -f 2)

Difference=$($CollegeMin-$HighSchoolMin | bc)

echo The difference in minimum wages between high school and college graduates is $Difference.

when i bash this.script i get line 13: 10.128063739-0.1028907398: command not found

I think this is because the variables are being considered strings not numerical values. I have tried declare -i but that does not work because of the decimal places, and I need an exact answer.

Does anyone know of any workarounds? am i on the right track or is there just a fundamental issue in how i'm going about this?

  • Now being from the US, how do you define College vs Highschool? – Stephen Quan Oct 9 at 18:47
  • 1
    @StephenQuan high school is grade 12, typically a bachelors would be grade 16. Guess I should've made that clearer. thanks for pointing it out – sethparker Oct 9 at 18:53
1

Shell scripts don't have numerical variables at all so that's not a problem. The error message is 100% correct 10.128063739-0.1028907398 is not a command, you need one to create the output to send to bc.

Try:

Difference=$(echo "$CollegeMin-$HighSchoolMin" | bc)

instead

  • wow, thanks! as usual, can't believe how simple the solution is... – sethparker Oct 9 at 18:51
0

I see @davolfman has the answer you needed.

FYI, you can use regular expressions with a capturing group on wages to optimize the retrieval of High School Grads (Grade 12) and College Grads (Grade 16) wages:

sed 's/^[^,]*,[^,]*,12,\(.*\)/\1/;t;d' wages.csv # High School Grad Wages
sed 's/^[^,]*,[^,]*,16,\(.*\)/\1/;t;d' wages.csv # College Grad Wages

Here's the updated script for HighSchoolMin and CollegeMin:

HighSchoolMin=$(sed 's/^[^,]*,[^,]*,12,\(.*\)/\1/;t;d' wages.csv | sort -n | head -1)
CollegeMin=$(sed 's/^[^,]*,[^,]*,16,\(.*\)/\1/;t;d' wages.csv |sort -n | head -1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.