Visit Our UNIX and Linux User Community


Shell Sript


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Shell Sript
# 1  
Old 01-11-2008
Shell Sript

Hi All,

I have four different files in four different directories.
Each file contains exactly the same format
logincode Forename Surname TutGroup Mark

Basically i want to grab all the marks from each file and put them onto the end of one login code by using a shell script. I can grab all the marks just by
cut -f 5 -d ' ' file1 file2 file3 file3

But then i want to add it on the end of the correct login code so i would have
logincode mark1 mark2 mark3 mark4

I have tried using join but can only do this for 2 files (i have 4) and it doesn't work if there is more than one record? i have tried
join -v 2 file1 file2 but this only displays record two.


Hope that makes sense
Regards
Jason
# 2  
Old 01-11-2008
Code:
 awk '{c[$1]=c[$1]" "$5 }END{for (i in c){print i,c[i]}}' file1 file2 file3 file4

# 3  
Old 01-11-2008
Your an absolute genius! It works perfectly!!!!
I don't really understand it all fully though. Could you explain?
Also if you wanted to do it without loops how could this be done?
I know how to use awk to calculate an average number for the whole file but how could i do this for one line? For example
50 60 70 (average mark here)
40 30 20 (average mark here)
Thanks


Quote:
Originally Posted by Tytalus
Code:
 awk '{c[$1]=c[$1]" "$5 }END{for (i in c){print i,c[i]}}' file1 file2 file3 file4


Last edited by jazz8146; 01-11-2008 at 02:02 PM..
# 4  
Old 01-11-2008
it creates an array for each firstfied, and adds on every 5th field where the firstfield atches... (the " " is the concatenation operator)

hmm - maybe not the clearest...hope the following makes more sense:

create array of values based on first col with concat 5th col values:
Code:
{c[$1]=c[$1]" "$5 }

Then loop through array and print out values:
Code:
{for (i in c){print i,c[i]}}'

for the average you could do:

Code:
 awk '{c[$1]=c[$1]" "$5 }END{for (i in c){print i,c[i]}}' file1 file2 file3 file4 | awk '{t=0;for (i = 2; i<=NF; i++) t+=$i;print $0,t/(NF - 1)}'

(i.e loop over 2nd through end cols, add 'em up and spit out the orig file with the avg tagged on...)
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #339
Difficulty: Easy
sftp is less secure than ftp for transferring files between computers.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass C shell array to another C shell script(csh) and shell(sh)

Dear Friends, Please help me on this my script name is send.csh In this i have written the statement like this set args = ( city state country price ) I want to pass this array to another c shell called receiver.csh. and i want to use it in this c shell or how to pass to... (2 Replies)
Discussion started by: SA_Palani
2 Replies

2. Shell Programming and Scripting

Monitoring Sript giving random end result

Hi Guys, I am developing a script to monitor GUI based FileNet Component "Component Manager" which logs it's running status in a log file. Log file is a huge file so in script I put last 300 lines of log file in seperate file and run script every 5 minutes. I am searching the string... (2 Replies)
Discussion started by: dhirajdsharma
2 Replies

3. Shell Programming and Scripting

compare dates using shell sript

I have 2 date feilds 2011-05-13:18:45 2011-05-13:18:30 I need to compare them and say its OK/NOK I tried this but dint work. systime=2011-05-13:18:45 shubtime=2011-05-13:18:30 if then echo" OK" else echo "NOK" fi In this its not same so the o/p should be NOK (2 Replies)
Discussion started by: LavanyaP
2 Replies

4. Shell Programming and Scripting

calling 'n' number of shell scripts based on dependency in one shell script.

Hello gurus, I have three korn shell script 3.1, 3.2, 3.3. I would like to call three shell script in one shell script. i m looking for something like this call 3.1; If 3.1 = "complete" then call 3.2; if 3.2 = ''COMPlete" then call 3.3; else exit The... (1 Reply)
Discussion started by: shashi369
1 Replies

5. Shell Programming and Scripting

Help need to make a shell script run for ffmpeg vhook watermaking in shell

i have a small problem getting a batxh shell script to run in shell this is the code the problem seems to be centered around the ffmpeg command, something maybe to do with the ' ' wrapping around the vhook part command this is a strange problem , if i take the ffmpeg command and... (1 Reply)
Discussion started by: wingchun22
1 Replies

6. Linux

How to Start a Shell as Login shell instead of ordinary shell

Hi I tried with bash --login option. but the output is siva:~$ bash --login siva:~$ is there any way to make the shell ask for user id and password ( and login as different user instead of using sudo / su ) Thx in advance Siva (3 Replies)
Discussion started by: Sivaswami
3 Replies

7. Shell Programming and Scripting

convert the below perl sript to shell script

perl script: my $logdir = '/smp/dyn/logfiles/fsm/mp/mp'; $logdir = $logdir ."/mp${toDate}*"; i tried to make it..as below .. but not working .. date +%m%d%y logdir = /smp/dyn/logfiles/fsm/mp/mp logdir=$logdir/mp"$date" but it was not working..... can someone please help me out in... (1 Reply)
Discussion started by: mail2sant
1 Replies

8. UNIX for Advanced & Expert Users

Sript need, please respond

Hi I am looking for the script which can move 1month old data from a TXT file.actully in this file data is appended on daily basis.pleasehalp me out. Here is the file content : $head abc.txt. 20070301130052,xxz. 20070307132111,cvasjchgjhcg gacg chjbgasjkchjk.... (3 Replies)
Discussion started by: vpandey
3 Replies

9. Shell Programming and Scripting

Shell Sript - Spell Checker Assign

Hi Folks. I am currently working on a script that has to spell check a file and print the output to the screen in 2 columns like this. INCORRECT CORRECTION whio who weahter weather The file will allow the user to override the ispell command and save any... (9 Replies)
Discussion started by: ccfc1986
9 Replies

10. AIX

Difference between writing Unix Shell script and AIX Shell Scripts

Hi, Please give me the detailed Differences between writing Unix Shell script and AIX Shell Scripts. Thanks in advance..... (0 Replies)
Discussion started by: haroonec
0 Replies

Featured Tech Videos