この質問を参照してください: テキストファイルから欠落している値を見つける
次のデータを含むファイルが2つあります。
はい
Name Feature
Marry Lecturer
Marry Student
Marry Leader
Bob Lecturer
Bob Student
Som Student
特徴
Lecturer
Student
Leader
以下のコードに従って、サンプルファイルの名前に欠けている関数を見つけました。
#!/bin/bash
rm -f *.missing names.all
feature=feature
sed -n '1!p' example.txt | cut -d ' ' -f 1 | sort -u > names.all
for i in $(cat $feature)
do
fgrep $i example.txt | cut -d ' ' -f 1 | cat - names.all | sort | uniq -u > $i.missing
done
このコードは、この機能のない名前をすべて含むLecturer.missing、Student.missing、およびLeader.missingの3つのファイルを提供します。
ただし、データが同じファイルにあることを望み、出力は次のようになります。
次の出力が必要です。
Lecturer Student Leader
Som bob
Som
同じファイルにデータを追加しようとしましたが、動作しません。
ベストアンサー1
このコード
awk '
NR == FNR {feature[$1]=1; next}
$1 != "Name" {name[$1]=1; role[$1,$2]=1}
END {
for (f in feature)
printf "%-12s", f
print ""
for (n in name) {
for (f in feature)
printf "%-12s", (n SUBSEP f in role ? " " : n)
print ""
}
}
' features roles
この出力を提供します
Lecturer Student Leader
Bob
Som Som
十分近いですか?