Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2003

    Unanswered: Extract lines from a file

    I am new to unix shell programming. I would like to know how I can do the following:
    Have a file contain the number of students and their marks separated by spaces so it's something like this:
    Johnny Blake 2255521455 50 100 85 95 100 85 20 51

    Now I want the shell script to display the name of the student, and his/her overall mark when the user inputs the exact same student number as the one in that file. Is there any way to do this in the Bourne Shell??? THanks in advance.

  2. #2
    Join Date
    Jan 2003
    You may want to get other ideas, but this is one way....
    #! /bin/ksh

    echo "Which Student?: \c"
    read student_no
    echo "Looking For Student ($student_no)...."

    awk -v sno=$student_no '{if ($3==sno) {printf "%-s,%-s\t%-6.2f\n",$2,$1,$4+$5+$6+$7+$8+$9+$10+$11;}}'< /your_filename
    awk -v sno=$student_no '$3==sno {printf "%-s,%-s\t%-6.2f\n",$2,$1,$4+$5+$6+$7+$8+$9+$10+$11;}' < your_filename
    NB EACH awk line should be one line, when I pasted the commands the 'editor' split the lines in the middle of the awk printf statement...the line should be continuous from 'awk -v .....' to '<your_filename' in both cases
    The script above (between the -------) will ask the user to enter the student no, then echos back which student it's looking for before passing the student no to a unix utility called awk. The awk script then sets its own variable (sno) to the entered student no and matches the student no to the 3rd field ($3) in your file.
    Two variants of the awk script do the same thing - the first shows an 'if' statement, and the second the built in pattern matching facility - both effectively filter the lines in your file to the ones matching the student no.
    Points to note/be wary/future reading/investigation:
    1) The script heading denotes KORN (#! /bin/ksh) shell - this may not be available on you Unix installation. BOURNE (#! /bin/sh) could be substituted if your machine supports that. Other shells are available.
    2) The student number entered is not validated in any way. The user could enter alphanumerics etc
    3) No error trapping is in the script. If the user hits interrupt - what do you want to do?
    4) There is no graceful way to exit the script.
    5) The script only tries to find one student at a time - you may want to filter against another file of input student numbers or loop on accepting the student no from the user (the read statement)
    6) The awk script will show ALL students matching that number. If there are 4 lines in the file with that number all 4 will be printed. It does not stop after the first one found, and it does not total all marks across mutiple records.It also does not report errors if the student is not found.
    7) The awk script (between the '....') could be put into a file and then be called using the awk -f file_name option (see AWKPATH environment variable in awk) This way you start to build a library of useful awk scripts. You can then format the awk script like a normal programming language - ie indent the commands etc..........
    8) The awk script does not do any 'nice' formatting apart from using the printf command to format the line(s) found. You may also want to print headers and footers etc (see awk BEGIN and END commands).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts