I need to search the value of each student attendance from below table and need to do summation of each student attendance.
I have a file dump with below data inside file_dump.txt
[days Leaves PERCENTAGE student_attendance]
194 1.3 31.44% student1.entry2
189 1.3 30.63% student2._student2
138 0.9 22.37% student3.entry2
5 0.0 0.81% student3._student3
5 0.0 0.81% student1._student1
I need to search student1 data from above table using linux command (grep or other commands) and then do the summation of student1.entry2 and student1._student1 together that is ( 194 5 = 199).
How can I do this using linux command line ?
CodePudding user response:
Awk is eminently suitable for small programming tasks like this.
awk -v student="student1" '$4 ~ "^" student "\." { sum = $1 }
END { print sum }' file
The -v
option lets you pass in a value for an Awk variable from the command line; we do that to provide a value for the variable student
. The first line checks whether the fourth field $4
begins with that variable immediately followed by a dot, and if so, adds the first field $1
to the variable sum
. (Conveniently, uninitialized variables spring to life with a default value of zero / empty string.) This gets repeated for each input line in the file. Then the END
block gets executed after the input file has been exhausted, and we print the accumulated sum.
If you want to save this in an executable script, you might want to allow the caller to specify the student to search for:
#!/bin/sh
# Fail if $1 is unset
: ${1?Syntax: $0 studentname}
awk -v student="$1" '$4 ~ "^" student "\." { sum = $1 }
END { print sum }' file
Notice how the $
variables inside the single quotes are Awk field names (the single quotes protect the script's contents from the shell's variable interpolation etc facilities), whereas the one with double quotes around it gets replaced by the shell with the value of the first command-line argument.