如何编写bash脚本,将任何.txt文件与下面列出的SAME格式进行比较,并使用grep输出?
第一列是一组ID,每个ID的分数都是1或0。ID 123有2次尝试,这就是为什么我把它除以6而不是3。
请注意,我不一定要比较file1.txt和file2.txt。假设有更多的.txt文件使用这种格式,你正试图找到不同ID的平均值(有些文件在多个文件中,如ID 123(。
file1.text
CourseA
123 1 1 1
456 1 0 1
789 0 0 0
file2.txt
CourseB
123 1 0 1
233 0 1 0
423 0 0 1
我想要的输出
ID CourseA CourseB AVG
123 3/3 2/3 5/6
456 2/3 - 2/3
789 0/3 - 0/3
233 - 1/3 1/3
423 - 1/3 1/3
#!/bin/bash
file='./file*.txt'
# list of headers
columns=$(head -qn1 $file | grep -oE '^S+' | sort | uniq)
# list of items
items=$(tail -qn+2 $file | grep -oE '^S+' | sort | uniq)
# print table header
echo "IDt${columns//$'n'/$'t'}tAVG"
# generate table sequencial
for item in $items
do
unset i j
# summate values overall files
for n in $(grep -h ^$item $file | grep -oE '(s+[0-9]+)')
do
i=$((i+n))
j=$((j+1))
done
# print next line
printf "%st" $item
# print columns
for column in $columns
do
# query all files for columns
grep -q ^$column $(grep -l ^$item $file)
if [ $? != 0 ]
then
# item does not exist in this file
printf "-t"
else
# print values for column
printf "%d %d %dt" $(grep -h ^$item $(grep -l ^$column $file) | grep -oE '(s+[0-9]+s+[0-9]+s+[0-9]+)')
fi
done
# print sum
printf "%d/%dn" $i $j
done