🗒️awk实现类VLOOKUP的功能(取交集)
2023-3-2
| 2023-4-10
0  |  0 分钟
type
status
date
slug
summary
tags
category
icon
password

1. 需求

  • a.txt是基础文件,已去重处理,大概十万行
  • b.txt是大量的log文件,一个文件可能20W行,5MB左右,数量是几百甚至几千个,即总行数是千万级。
需求:如果a.txt的字符串出现在b.txt中,就打印输出,听着很像VLOOKUP,但是这么大的文件数量用Excel不现实
#文本样例 [root@e1zone shell]# cat a.txt ad123456 ad123457 ad123458 ad123410 ad123434 [root@e1zone shell]# cat b.txt 16334400 ad123456 1 16334401 ad123456 10 16334401 ad123457 2 16334402 ac123457 2 16334405 ad123457 5

2. awk的方案

实测:64G内存的服务器,3000W+ 行log日志,耗时1min6s ,而传统的for循环耗时是4个小时左右
[root@e1zone shell]# awk 'NR==FNR{a[$1]=$0;next}{if($2 in a)print}' a.txt b.txt 16334400 ad123456 1 16334401 ad123456 10 16334401 ad123457 2 16334405 ad123457 5
 
技术
  • scripts
  • awk根据key列相同值取value列最大最小值阿里sls采集logstash处理过的日志
    目录