简体   繁体   English

将文件 1 中的字符串与文件 2 中的字符串匹配

[英]Match string in file1 with string in file2

my data examples are 
1.txt
MTQZ3CODT0SQKGE3QE6B | j t | j | t | 22312 | stimpy | EST | 8 | 20 | text | list | 0 | | 2002-08-22 13:07:05

2.txt
 MTQZ3CODT0SQKGE3QE6B | joe@example.com

desired output 
joe@example.com | j t | j | t | 22312 | stimpy | EST | 8 | 20 | text | list | 0 | | 2002-08-22 13:07:05

I suppose to match & replace 1st column from 1.txt with 2nd column in 2.txt so far i did try :我想用 2.txt 中的第二列匹配和替换 1.txt 中的第一列,到目前为止我确实尝试过:

awk 'BEGIN { while((getline < "file2.txt") > 0) a[$1]=$3 } { $1 = a[$1] } 1' file1.txt

Its work well but after 12hours of running i just finalise only 1GB looks very slow它运行良好,但运行 12 小时后,我只完成了 1GB 看起来很慢

INFO: file1.txt=7GB  file2.txt=4GB my memory 16GB

I'm not sure what cause the slowly thing but i hope if there's another fast way then i'm using of awk will be helpfull.我不确定是什么导致了缓慢的事情,但我希望如果有另一种快速的方法,那么我使用的 awk 会有所帮助。
Thanks!!谢谢!!

Note: I'm running out of memory is there another way to do it and that's to not have an array at all?注意:我的内存不足有没有其他方法可以做到这一点,那就是根本没有数组? Also in my case lines are randomly and not in the same lines!同样在我的情况下,线条是随机的,不在同一条线上!

$ join <(sort 2.txt) <(sort 1.txt) | cut -d' ' -f3-
joe@example.com | j t | j | t | 22312 | stimpy | EST | 8 | 20 | text | list | 0 | | 2002-08-22 13:07:05

If that's not all you need then edit your question to provide more truly representative sample input/output including cases that this doesn't work for.如果这不是您所需要的全部,那么编辑您的问题以提供更真实的代表性样本输入/输出,包括这不起作用的情况。

You may use this awk :你可以使用这个awk

awk -F ' *\\| *' -v OFS=' | ' '
FNR == NR {
   map[$1]=$2
   next
}
$1 in map {
   $1 = map[$1]
} 1' 2.txt 1.txt
joe@example.com | j t | j | t | 22312 | stimpy | EST | 8 | 20 | text | list | 0 |  | 2002-08-22 13:07:05

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在基于file1的file2中搜索字符串并替换 - Search for a string in file2 based on file1 and replace bash 命令在文件 2 中的特定字符串/文本之后将文件内容从文件 1 复制到文件 2 - bash command to copy file contents from file1 to file2 after particular string/text in file2 如果file1的第一列与file2中的任何字符串匹配,则将其替换为file1的第二列 - If the first column of file1 matches any string in file2, then replace it with the second column of file1 需要用file1的第二列替换与file1的第一列匹配的file2中的字符串 - Need to replace string in file2 that matches first column of file1 with second column of file1 匹配文件1和文件2中的数据范围 - match data range from file1 with file2 将File1与File2合并(继续从File1追加到File2,直到不再有行) - Merge File1 with File2 (keep appending from File1 to File2 until no more rows) 理解大括号对重定向的影响 (&quot;&gt; file1 &gt;&amp; file2&quot; vs. &quot;{ &gt; file1; } &gt;&amp; file2&quot;) - Understanding the effect of curly braces with respect to redirections ("> file1 >& file2" vs. "{ > file1; } >& file2") 比较2个文件并删除file2中与file1中找到的值匹配的任何行 - Compare 2 files and remove any lines in file2 when they match values found in file1 AWK 根据文件 1 中的部分标题匹配选择文件 2 中的列 - AWK select columns in file2 based on partial header match in file1 sed用file1中的file3内容替换file2内容 - sed replace file2 content with file3 content in file1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM