Shell Script to read a tab delimited file and perform simple tasks

  1. The problem statement, all variables and given/known data:
    Hello!
    I need help with this problem bash shell scripting that basically just reads the data in a tab delimited file and does the following below

  2. Read in the data file Survey.txt and assign the column values to variables of your choosing.

  3. Calculate the total number of survey respondents.

  4. Calculate the ratio of male to female respondents.

  5. Calculate the average Height and Weight of the male and female respondents and compare your results.

  6. Calculate the average Verbal and Math scores of the right- and left-handed respondents and compare your results.

  7. Relevant commands, code, scripts, algorithms:
    Attached is the Survey.txt file that this script is going to be reading.

  8. The attempts at a solution (include all code and scripts):
    Here is the code that I have completed so far:

#!/bin/bash
IFS=$'\n'
for line in $(cat ./Survey.txt)
do
   echo $line
done
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    Eastern Center For Arts and Technology Willow Grove PA USA Computer Network Administration Instructor Karon Crickmore

IFS is used to tell the shell what the field separator character is. \n is normally the record separator. The file has tab characters to separate the fields - how do you write a tab?

Are you allowed to use awk?

From what I got 'IFS=$'\n' is the way of telling the shell that the file being read is tab delimited.

Yes I was going to use the awk command to parse and separate the file. We are allowed to use any command that is in the bash shell.

Hi, do you know what \n stands for and what \t stands for? Anyway I would forget about a for loop with a global IFS. I would look into while read loop in which you can also set IFS local to the read statement if need be, plus it gives the added possibility of reading more variables in on go...

Can you post some data from Survey.txt?

Sure. Here is a snip-it of Survey.txt

Sex    Math    Verbal    Height    Weight    Handed
male    640    470    71    210    left
female    660    650    65    135    right
male    550    580    68    145    right
female    560    660    67    135    right
female    600    790    69    164    right
female    560    640    71    175    right
female    550    660    64    120    left
female    600    560    63    103    right
female    540    560    67    150    right
female    540    540    65    130    right
female    600    620    64    140    right
male    670    640    74    175    left
female    680    550    64    135    right

Use a while loop to read the file. You now have all the values in 6 named variables that you can use to perform the math mentioned in your problem statement.

#!/bin/bash

while read sex math verbal height weight handed
do
        [ "$sex" == "Sex" ] && continue                       # Skip the Header
        echo $sex $math $verbal $height $weight $handed       # Print values read in variable
done < survey.txt

I hope this helps.