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
1. Read in the data file Survey.txt and assign the column values to variables of your choosing.
2. Calculate the total number of survey respondents.
3. Calculate the ratio of male to female respondents.
4. Calculate the average Height and Weight of the male and female respondents and compare your results.
5. Calculate the average Verbal and Math scores of the right- and left-handed respondents and compare your results.
2. Relevant commands, code, scripts, algorithms:
Attached is the Survey.txt file that this script is going to be reading.
3. The attempts at a solution (include all code and scripts):
Here is the code that I have completed so far:
4. 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?
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...
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.
Code:
#!/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
Hi, I have a rquirement in unix as below .
I have a text file with me seperated by | symbol and i need to generate a excel file through unix commands/script so that each value will go to each column.
ex:
Input Text file:
1|A|apple
2|B|bottle
excel file to be generated as output as... (9 Replies)
Hi Experts,
I have a tab deliminated file as below
myfile.txt
Local Group Memberships *Administrators *Guests
I need data in below format starting from 4th position.
myfile1.txt
Administrators Guests
the above one is just an example and there could... (15 Replies)
Hi How to make tab delimited file to space delimited?
in put file:
ABC kgy
jkh ghj
ash kjl
o/p file:
ABC kgy
jkh ghj
ash kjl
Use code tags, thanks. (1 Reply)
I have a file which was pipe delimited, I need to make it tab delimited. I tried with sed but no use
cat file | sed 's/|//t/g'
The above command substituted "/t" not tab in the place of pipe.
Sample file:
abc|123|2012-01-30|2012-04-28|xyz
have to convert to:
abc 123... (6 Replies)
Hi Forum
I have a tab delimited file that opens well in Openoffice calc (excel). But when I perform any operation in command line, it reads the file incorrectly. When I 'save As' the same file in office as tab delimited then it works fine.
The file that I think is tab delimited is actually... (8 Replies)
Hi:
I'm writing a script that will take source / destination pathnames and metadata information from a tab-delimited input file, and then perform various directory creation, file moving and renaming, and tagging of files.
I think I have what I need to do the file manipulation in the script -... (4 Replies)
Hi
I am trying to retrieve values from a tab-delimited file.I am using
while read record
value=`echo $record | cut -f12`
done
Where 12 is the column no i want retieve and record is one line of the file.
But it is returning the full record.
Plz help (4 Replies)
OK, let's set this up. I have a tab delimited file from excel. In my UNIX shell I have the following lines
IFS=`printf "\t"`
while read LINE_NO SKIP IGNORE_ERRORS OTHER
do
....
This works fine if there is something in every column like this. NOTE, those are tabs, not spaces. :)
... (2 Replies)