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?

3
  • Now being from the US, how do you define College vs Highschool? Oct 9, 2019 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, 2019 at 18:53
  • By that rule there is no-one in your sample input file with a bachelors since the highest years of school shown is 14 and you didn't provide any expected output so we don't have anything to test a potential solution against. Although you got an answer to your question what you're doing should just be one tiny awk script instead of a shell script calling a bunch of tools multiple times in pipes, etc. Ask a new question and tag it with awk if you'd like help doing this the right way.
    – Ed Morton
    Feb 14, 2021 at 13:30

2 Answers 2

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

1
  • wow, thanks! as usual, can't believe how simple the solution is...
    – sethparker
    Oct 9, 2019 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)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .