我有 2 個腳本file.txt和file2.txt
檔案1.txt
name|mandatory|
age|mandatory|
address|mandatory|
email|mandatory|
country|not-mandatory|
檔案2.txt
gabrielle||nashville|[email protected]||
這些是我的確切資料檔案,在 file1 中,column1 是欄位名稱,column2 是注意該欄位在 file2 中是否不應該為空。
在 file2 中,資料位于由 | 分隔的單行中。file1 中提到的強制年齡在 file2 [這是單行] 中不存在,這也是我需要的輸出。
預期輸出:
age mandatory
我得到了 file2 與 file1 格式相同的代碼,其中強制替換為 field2 資料。
awk -F '|' '
NR==FNR && $3=="mandatory" {m[$2] }
NR>FNR && $3=="" && m[$2] {printf "%s mandatory\n", $2}
' file1.txt file2.txt
uj5u.com熱心網友回復:
您必須遍歷 fields for(... i <= NR ...)。
awk -F '|' '
NR==FNR { name[NR]=$1; man[NR]=$2 }
NR!=FNR {
for (i = 1; i <= NR; i) {
if ($i == "" && man[i] == "mandatory") {
printf("Field %s is mandatory!\n", name[i]);
}
}
}
' file1.txt file2.txt
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/467387.html
