LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-29-2014, 12:38 AM   #1
packirisamy
LQ Newbie
 
Registered: Jul 2014
Posts: 3

Rep: Reputation: Disabled
Unhappy Manipulate student records in shell script


I have a file student.dat with enteries like

ID NAME SUB1 SUB2 SUB3 SUB4 SUB5
1 adi 10 10 20 20 20

like this i have many entries. Now i need to find the total marks of all the student in another file say tot.dat. And the subject average in file named avg.dat.
Then i need to allocate grades for all the students in another file say grade.dat with entry like

ID Name Grade
1 adi A

CAN SOMEONE HELP ME???
 
Old 07-29-2014, 12:51 AM   #2
notKlaatu
Member
 
Registered: Sep 2010
Location: Wellington, New Zealand
Distribution: Slackware, Fedora
Posts: 921

Rep: Reputation: 585Reputation: 585Reputation: 585Reputation: 585Reputation: 585Reputation: 585
Your over all plan should go something like this:

1. sort the files so that each line is in the same order.
2. iterate (that is, loop) through the files line-by-line, doing this:
a. check the name to make sure the lines match as expected
b. extract the information you need.
c. dump the info into a new file.
3. move to the next file


There are lots of way to do this, but since it looks like you have pretty distinct fields to work with, I am thinking AWK (great tutorial on AWK here http://www.grymoire.com/Unix/Awk.html), although you could do it with cut, too.

Good luck!
 
Old 07-29-2014, 12:55 AM   #3
pwalden
Member
 
Registered: Jun 2003
Location: Washington
Distribution: Fedora FC23, Raspbian, Ubuntu Precise via Chrome/Crouton
Posts: 355

Rep: Reputation: 42
Sounds like you want someone to do your homework for you.

What do you want the solution in? Perl, awk, tcl, python, php, C, C++, Java, ... What is data source format? sqlite, mysql, mariaDB, csv, ...
 
Old 07-29-2014, 03:20 AM   #4
eklavya
Member
 
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
Suppose your file is like this :
Code:
ID NAME SUB1 SUB2 SUB3 SUB4 SUB5
1 adi 10 10 20 20 20
2 ben 15 16 13 12 15
3 cob 15 14 18 19 16
4 den 85 86 84 85 89
5 elf 78 45 25 65 23
6 foe 78 58 56 52 48
7 git 48 46 42 15 76
8 hen 78 45 12 16 19
9 ink 85 86 82 15 14
10 joe 49 46 13 15 18
And you want total and average of every student.
Code:
Run the following script.
#!/bin/bash
awk '
BEGIN {FS=OFS=" "}
{
sum=0; n=0
for(i=3;i<=NF;i++)
     {sum+=$i; ++n}
     print $0,"Sum:"sum,"Average:"sum/n
}' file.txt
In this example, first subject is third column that's why i=3
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to manipulate shell variable in awk me_spearhead Programming 5 05-25-2012 10:05 AM
how to manipulate shell variable in awk me_spearhead Programming 2 05-25-2012 03:21 AM
[SOLVED] shell script to get count of records in a mysql table Quakeboy02 Programming 3 10-24-2010 11:21 AM
How to compare records in two tables in seperate My Sql database using shell script sumitarun Programming 5 04-14-2005 10:45 AM
Import records to MySQL Database from a shell script chrisk5527 Linux - General 12 03-24-2004 10:49 PM


All times are GMT -5. The time now is 06:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration