[英]How do I check if a field is empty or null in a text file using awk and bash
I have two text files and I want to compare their correspondent values according to their rows and columns. 我有两个文本文件,我想根据它们的行和列比较它们的对应值。 Each value (field) in the text file is separated by tabs. 文本文件中的每个值(字段)都用制表符分隔。
Here are the files: 这些是文件:
file1.txt FILE1.TXT
Name Col1 Col2 Col3
-----------------------
row1 1 4 7
row2 2 5 8
row3 3 6 9
file2.txt FILE2.TXT
Name Col1 Col2 Col3
-----------------------
row2 1 4 11
row1 2 5 12
row3 3 9
Here is the code I have so far: 这是我到目前为止的代码:
awk '
FNR < 2 {next}
FNR == NR {
for (i = 2; i <= NF; i++) {
a[i,$1] = $i;
}
next;
}
# only compare if a row in file2 exists in file1
($1 in b) {
for (i = 2; i <= NF; i++)
{
if (a[i,$1] == $i)
{
print "EQUAL"
}
else if ( //condition that checks if value is null// )
{
print "NULL"
}
else
{
print "NOT EQUAL"
}
}
}' file1.txt file2.txt
I am having difficulties with checking if there is a null value ( row3
and col2
in file2.txt
) in file2.txt
. 我在检查file2.txt
是否存在空值( file2.txt
row3
和col2
)时file2.txt
。 I don't even get an output for that null value. 我什至没有得到该空值的输出。 So far I tried if ($i == "")
and it is still not giving me any output. 到目前为止,我尝试了if ($i == "")
,但它仍然没有给我任何输出。 Any suggestions? 有什么建议么? Thanks. 谢谢。 (I'm using gnu awk in a bash script) (我在bash脚本中使用gnu awk)
Let me know if further explanation is required. 让我知道是否需要进一步说明。
只需将FS设置为tab:
awk -F'\t' '....'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.