![](/img/trans.png)
[英]Bash script to read txt file, do a while loop and write output
[英]How do I write a bash script to compare and output values in any .txt files that have the same format?
如何編寫 bash 腳本來比較 any.txt 文件與我在下面列出的相同格式和 output 使用 grep 它們?
第一列是一堆 ID,每個都有 1 或 0 的分數。ID 123 有 2 次嘗試,這就是為什么我除以 6 而不是 3。
請注意,我不是必須將 file1.txt 與 file2.txt 進行比較。 假設有更多具有這種格式的 .txt 文件,您正試圖找到不同 ID 的平均值(有些文件位於多個文件中,例如 ID 123)。
文件1.txt
CourseA
123 1 1 1
456 1 0 1
789 0 0 0
文件2.txt
CourseB
123 1 0 1
233 0 1 0
423 0 0 1
我想要的 Output
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 "ID\t${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 "%s\t" $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 %d\t" $(grep -h ^$item $(grep -l ^$column $file) | grep -oE '(\s+[0-9]+\s+[0-9]+\s+[0-9]+)')
fi
done
# print sum
printf "%d/%d\n" $i $j
done
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.