我有两个文件是在执行几个脚本并运行少量查询后得到的,这两个文件的差异(每个文件有两列,名称和编号)现在存储在第三个文件中 第三个文件包含三列!
! <Employee Name1> <Employee Number1>
! <Employee Name1> <Employee Number2>
! <Employee Name3> <Employee Number4>
! <Employee Name3> <Employee Number5>
如何一次读取2行文件并报告类似
的内容<Employee Name1> has different Employee ID.It is <Employee Number1> in File1 and <Employee Number2> in File2
答案 0 :(得分:1)
使用bash,你可以在while循环中读取2行:
while read -r _ name num1; read -r _ name num2; do
echo "$name has different Employee ID.It is $num1 in File1 and $num2 in File2"
done < file3
答案 1 :(得分:0)
正如@shellter建议的那样,几乎可以肯定有更好的方法来实现你所描述的目标,但本着“你要求的小心”这一点,这里有一个awk程序这就是你所要求的(通过一点点理智检查: - ):
awk -F'[<>]' '
function reset() { employee=e1="" }
NR % 2 { employee=$2; e1 = $4; next; }
{ if ($2 == employee) {
print employee " is " e1 " in File1 and " $4 " in File2";
} else {
print "INTERNAL ERROR at line " NR ": " employee " vs " e1 "; resetting ...";
}
reset();
}'
答案 2 :(得分:0)
假设名称和ID不包含空格(根据问题评论中的示例):
awk '
$2 == name && $3 != id {
print name, id, $3
}
{
name = $2
id = $3
}
'
答案 3 :(得分:0)
AWK
awk 'NR==FNR{arr[$1]=$2;next}{if(($1 in arr) && !(arr[$1]==$2)){print $1, "has a different Employee ID.It is",arr[$1],"in file 1 and ",$2,"in file 2" }}' file1 file2
其中file1和file2是文件的路径
例如
文件1
John 1357
Joe 2362
file2的
John 1357
Joe 2361
Joey 25135
Julius 1235
输出
Joe has a different Employee ID.It is 2362 in file 1 and 2361 in file 2