例如,如果我有数字1068
File A has
1094 A B C
1068 D E F
1044 G H I
File B has
1092 L M N
1068 X Y Z
1045 Q R S
File C has
1093 A B C
1062 D E F
1041 G H I
我想要从所有文件中获取1068行,只粘贴某些列,并将它们并排粘贴。请注意,文件C没有1068,但我想要粘贴NA。所以最终输出看起来像这样:
1068 FileA A C FileB X Z FileC NA NA
任何帮助将不胜感激!我现在不知道如何grep列,甚至检查它是否存在。例如在文件C中,grep只是没有任何东西,但我希望在NA NA中代替。我该怎么做?
答案 0 :(得分:0)
我不认为这是grep
的工作。更多awk
工作。
awk -v num=1068 '
BEGIN { printf "%d", num }
# If file has changed and num has not been found...
FNR==1 && NR!=1 && !found_num { printf " %s NA NA", FILENAME }
# If at the beginning of a file (needs to be after previous line).
FNR==1 { found_num = 0 }
# If we find num, print the data and set found_num flag.
$1 == num { printf " %s %s %s", FILENAME, $2, $4; found_num = 1 }
END { if (!found_num) printf " %s NA NA", FILENAME; print"" }
' FileA FileB FileC