49.2k views
5 votes
Create a file named students containing the following data in your current directory. Each line in this file represents a student's first name, last name, and 3 exam scores. Each line should be regarded as a record containing 5 fields separated by one or more tab characters. You must use the awk command to answer the questions below

1 Answer

6 votes

Answer:

#!/bin/bash

# Simple line count example, using bash

# Usage: ./line_count.sh file

# -----------------------------------------------------------------------------

# Link filedescriptor 10 with stdin

exec 10<&0

# stdin replaced with a file supplied as a first argument

exec < $1

# remember the name of the input file

in=$1

# init

file="current_line.txt"

let count=0

# this while loop iterates over all lines of the file

while read LINE

do

# increase line counter

((count++))

# write current line to a tmp file with name $file (not needed for counting)

echo $LINE > $file

# this checks the return code of echo (not needed for writing; just for demo)

if [ $? -ne 0 ]

then echo "Error in writing to file ${file}; check its permissions!"

fi

done

echo "Number of lines: $count"

echo "The last line of the file is: `cat ${file}`"

# Note: You can achieve the same by just using the tool wc like this

echo "Expected number of lines: `wc -l $in`"

# restore stdin from filedescriptor 10

# and close filedescriptor 10

exec 0<&10 10<&-

Step-by-step explanation:

User Steven Liao
by
5.2k points