Yet more shell scripting

Discussion in 'Programming/Scripts' started by Wraith, Apr 26, 2007.

  1. Wraith

    Wraith New Member

    I have been visiting this forum for a couple of weeks now, originally searching for shell scripting advice. Some threads here have been absolutely invaluable.

    I have finally got stuck in my Assignment for uni though, I am trying to write a script to calculate missed pracs for students.

    I have three files containing prac marks over a semester, they look a little like:

    #Prac Marks for class 1
    #fomat is SID MARK
    21353535 10
    24535365 6
    34213444 9

    I need to compile a file of missed pracs, where no mark is recorded in any particular prac.

    what I have so far is:

    cut Students -f1 -d: | grep -v \# > Missing-temp; #extract SID from the first column of my Students file.

    while read sid extra; do #read SID using extra to catch any extra stuff not needed

    if cut Prac1 -f1 -d ' ' | grep -v \# | grep "$sid"; then #cut field 1 out of Prac1, this is the SID field, remove any lines starting with # using grep then find whether the relevant SID is in the Prac file.
    echo "Found $sid" >>error; #just for debugging purposes
    else
    echo $sid: Prac1, >> Missing1; #this is my required format
    fi;

    if cut Prac2 -f1 -d ' ' | grep -v \# | grep "$sid"; then
    echo "Found $sid">>error;
    else
    echo $sid: Prac2, >> Missing2;
    fi;

    if cut Prac3 -f1 -d ' ' | grep -v \# | grep "$sid"; then
    echo "">>error;
    else
    echo $sid: Prac3, >> Missing3;
    fi;

    done < Missing-temp; #pipe Missing-temp into read, end loop

    join -a 1 -a 2 Missing1 Missing2 | join -a 1 -a 2 - Missing3 >> Missing4; #join files together, using -a to ignore any missing bits

    sed 's/,$//' <Missing4 | sort -g >Missing; # removes trailing commas and sorts numerically.

    rm -f Missing1 Missing2 Missing3 Missing4 Missing-temp; #get rid of temp files


    The script works fine on my test data but when I submit it, I get the following error:
    File: Missing
    Expected: 312349311: Prac1, Prac2
    Got: 312349311: Prac1

    I have tested for this kind of output in my test files and it works fine. Does anyone have any suggestions as to why the script is not working?
     
  2. ghostdog74

    ghostdog74 New Member

    you can use awk


    Code:
    awk -v file="Prac1" ' BEGIN {  # get all sids of Prac1 into array
    		while( (getline line < file )>0 ) { 
    			if (line ~ /^#/ ) continue #skip comments 
    			mprac[line]
    		}
          }
          /^#/ {next} #skip comments line in Students file
    	{ 		
    		if (!( $1 in mprac )) {
    			print "Sid: " $1 " not in " FILENAME
    		}
    	
    	}
    	
    ' "Students"
     
  3. Wraith

    Wraith New Member

    Thanks for the advice. I finally figured out that I needed to sort my Prac files before joining them *doh!*

    Oh, well
     

Share This Page