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


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Shell Script to read a tab delimited file and perform simple tasks
# 1  
Old 12-17-2012
Linux 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:
Code:
#!/bin/bash
IFS=$'\n'
for line in $(cat ./Survey.txt)
do
   echo $line
done


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
# 2  
Old 12-17-2012
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?
# 3  
Old 12-17-2012
Quote:
Originally Posted by jim mcnamara
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.
# 4  
Old 12-17-2012
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...
# 5  
Old 12-18-2012
Can you post some data from Survey.txt?
# 6  
Old 12-18-2012
Quote:
Originally Posted by jim mcnamara
Can you post some data from Survey.txt?
Sure. Here is a snip-it of Survey.txt

Code:
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

# 7  
Old 12-18-2012
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

I hope this helps.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace a column in tab delimited file with column in other tab delimited file,based on match

Hello Everyone.. I want to replace the retail col from FileI with cstp1 col from FileP if the strpno matches in both files FileP.txt ... (2 Replies)
Discussion started by: YogeshG
2 Replies

2. UNIX for Dummies Questions & Answers

Need to convert a pipe delimited text file to tab delimited

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)
Discussion started by: raja kakitapall
9 Replies

3. Shell Programming and Scripting

How to read data from tab delimited file after a specific position?

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)
Discussion started by: Litu1988
15 Replies

4. Shell Programming and Scripting

How to make tab delimited file to space delimited?

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)
Discussion started by: jagdishrout
1 Replies

5. Shell Programming and Scripting

Help with converting Pipe delimited file to Tab Delimited

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)
Discussion started by: karumudi7
6 Replies

6. UNIX for Dummies Questions & Answers

tab delimited file that is not tab delimited.

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)
Discussion started by: imlearning
8 Replies

7. Shell Programming and Scripting

Mac script - issue with tab delimited input file

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)
Discussion started by: GRIMESPACE
4 Replies

8. Shell Programming and Scripting

Retrieving values from tab-delimited file in unix script

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)
Discussion started by: akashtcs
4 Replies

9. UNIX for Dummies Questions & Answers

Read a tab delimited

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)
Discussion started by: podzach
2 Replies

10. Shell Programming and Scripting

Converting Tab delimited file to Comma delimited file in Unix

Hi, Can anyone let me know on how to convert a Tab delimited file to Comma delimited file in Unix Thanks!! (22 Replies)
Discussion started by: charan81
22 Replies
Login or Register to Ask a Question