按行匹配两个文件通过列线 - 无钥匙 [英] Match two files by column line by line - no key

查看:133
本文介绍了按行匹配两个文件通过列线 - 无钥匙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有80,000的长度相同的记录加上两个大文件。我需要通过该文件的第一8个字符到由线两个文件行比较。文件中的一个的一行是要比较排队文件中的两个中的一个。文件一二号线是被比作线上的两个文件中的两个的。

I have two large files of 80,000 plus records that are identical in length. I need to compare the two files line by line by the first 8 characters of the file. Line one of file one is to be compared to line one of file two. Line two of file one is to be compared to line two of file two.

样品文件1

01234567blah blah1
11234567blah blah2
21234567blah blah3
31234567blah blah4

样品文件2

31234567blah nomatch
11234567matchme2
21234567matchme3
31234567matchme4

2行 - 4应该匹配,但1号线不应该。我的脚本匹配第1行至第4行,但应该是比较公正行1。

Lines 2 - 4 should match but line 1 should not. My script matches line 1 to line 4 but should be compared to just line 1.

awk '                                                        
FNR==NR {                                                  
a[substr($0,1,8)]=1;next                                       
        }                                                              
{if (a[substr($0,1,8)])print $0; else print "Not Found", $0;} 
'  $inputfile1  $inputfile2     >  $outputfile1               

感谢您。

推荐答案

有关逐行比较,你需要使用 FNR 变量作为重点。尝试:

For line by line compare you need to use FNR variable as key. Try:

awk 'NR==FNR{a[FNR]=substr($1,1,8);next}{print (a[FNR]==substr($1,1,8)?$0:"Not Found")}' file1 file2
Not Found
11234567matchme2
21234567matchme3
31234567matchme4

这篇关于按行匹配两个文件通过列线 - 无钥匙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆