当前位置:网站首页>awk实现类sql的join操作
awk实现类sql的join操作
2020-11-08 16:12:00 【xindoo】
awk、grep和sed被称为linux三剑客,事实上grep和awk我在日常工作中也常用到(sed用的比较少),可能有些人对awk了解比较少,我先大概介绍下。 很多人以为awk只是一个文本处理工具,实际上他们也是这么用的。但事实上它其实是一门语言,拥有数学运算符、进程控制语句,甚至针对于文本处理封装了很多内置变量和函数,这造就了它强大的文本处理能力。 如果grep只能做到数据的筛选,那awk还能做到数据的处理、分析甚至生成报表,毕竟它是一门完整的编程语言。
因为这篇文章不是awk的入门教程,如果想入门的话我推荐阮一峰老师的awk 入门教程 和 左耳朵耗子的AWK 简明教程 。
回到我今天的正题,今天我给大家看个我常使用awk的场景。因为做后端开发,经常在做数据分析的时候会有这样的问题,1.面对几十万条的数据,需要筛选出几百几千个特定key的数据。2.对这几百万条数据,针对其中的id字段补齐其他字段。
这时候可能有精通excel的同学跳出来说 “就这,so easy,vlookup就搞定!” ,事实上,excel确实可以解决问题,但有点重,甚至有些时候我们在服务器上还用不了excel。还有啥其他方法?针对我说的这俩场景,其实仔细想一下,是不是sql中俩表join就能解决问题。实际上你并不需要真正把文件灌到数据库里,只需要用awk一条命令就能解决。
实例
我们把问题具像下,假设有两个文件,score.txt存着学号+成绩的数据,另外一个name.txt存着学号+姓名的数据,你现在想知道每个人都烤了多少分。
score.txt
id score
1 87
2 67
3 68
4 75
5 90
6 100
7 0
name.txt
id name
1 张三
2 李四
3 王五
4 赵二
5 刘能
6 熊大
你想得到一份包含学号 姓名和成绩的数据,就像下面这样。
id score name
1 87 张三
2 67 李四
3 68 王五
4 75 赵二
5 90 刘能
6 100 熊大
7 0
用awk生成这样的数据有多简单?只需要一行代码,你可以保存name.txt和score.txt,然后执行下面命令尝试下。
awk 'ARGV[1]==FILENAME {map[$1]=$2} ARGV[2]==FILENAME {print $0, map[$1]}' name.txt score.txt
简简单单就实现了name.txt和score.txt在id之上的right join。
解释下上面的代码,ARGV和FILENAME是awk内置的变量,ARGV里存放在awk所接受的参数列表,像上面 ARGV[1]就是"name.txt",ARGV[2]就是"score.txt"。awk是面向行的,所以针对每一行数据都会执行ARGV[1]==FILENAME {map[$1]=$2} ARGV[2]==FILENAME {print $0, map[$1]},每一行数据都会属于某个文件,FILENAME标识出当前行所属的文件名,像在括号{}前的ARGV[1]==FILENAME 你可以看做是其他语法中的条件判断,你可以认为它就是省略了if,但和if的功能是一致的。
所以上述代码的含义就是 如果当前行是输入name.txt的,就把学号和姓名存在map里(awk里的变量不用预先声明)。 如果当前行是属于score.txt,就从map里把姓名找出来,然后把数据输出。
结语
awk 'ARGV[1]==FILENAME {map[$1]=$2} ARGV[2]==FILENAME {print $0, map[$1]}' name.txt score.txt
针对于不同的数据,只需要调整下$后面的具体值,可以实现用不同的列作为key来做join,其实这里并不比sql的join复杂,但在linux服务器上却很方便。上网中我只是实现了right join,如果在print $0, map[$1]前加上if (length(map[$1]) > 0) 就可以实现inner join。left join的话也只需要把文件名换一下。
知道了这些,awk实现多文件的交集、差集等操作都不在话下。另外不要忘记了awk其实也是一门编程语言,所以它也可以实现很多很复杂的逻辑,你可以把代码在某个文件里然后用-f参数调起,比如我之前老师用awk做一些简单的统计工作,比如计算均值、总和…… awk简直堪称后端工程师提效利器。
版权声明
本文为[xindoo]所创,转载请带上原文链接,感谢
https://my.oschina.net/xindoo/blog/4707635
边栏推荐
- Travel notes of Suzhou
- Flink from introduction to Zhenxiang (7. Sink data output file)
- Flink从入门到真香(7、Sink数据输出-文件)
- I used Python to find out all the people who deleted my wechat and deleted them automatically
- Blockchain weekly: the development of digital currency is written into the 14th five year plan; Biden invited senior adviser of MIT digital currency program to join the presidential transition team; V
- This year's salary is 35W +! Why is the salary of Internet companies getting higher and higher?
- 擅长To C的腾讯,如何借腾讯云在这几个行业云市场占有率第一?
- 喜获蚂蚁offer,定级p7,面经分享,万字长文带你走完面试全过程
- Apache Kylin远程代码执行漏洞复现(CVE-2020-1956)
- Tencent, which is good at to C, how to take advantage of Tencent's cloud market share in these industries?
猜你喜欢

Flink: from introduction to Zhenxiang (3. Reading data from collection and file)
![[Python 1-6] Python tutorial 1 -- number](/img/3b/00bc81122d330c9d59909994e61027.jpg)
[Python 1-6] Python tutorial 1 -- number

How to make a correct summary for 7 years?

Flink from introduction to Zhenxiang (7. Sink data output file)

Examples of unconventional aggregation

关于update操作并发问题

Builder pattern

Recurrence of Apache kylin Remote Code Execution Vulnerability (cve-2020-1956)

金融领域首个开源中文BERT预训练模型,熵简科技推出FinBERT 1.0

Workers, workers soul, draw lifelong members, become a person!
随机推荐
. net large data concurrency solution
Python基础语法
Flink: from introduction to Zhenxiang (6. Flink implements UDF function - realizes more fine-grained control flow)
Alibaba cloud accelerates its growth and further consolidates its leading edge
喝汽水,1瓶汽水1元,2个空瓶可以换一瓶汽水,给20元,可以多少汽水
How to solve the difference between NAT IP and port IP
Rabbitmq (1) - basic introduction
非常规聚合问题举例
On the confirmation of original data assets
关于update操作并发问题
[open source]. Net uses ORM to access Huawei gaussdb database
I used Python to find out all the people who deleted my wechat and deleted them automatically
AI周报:允许“员工自愿降薪”;公司回应:员工内心高兴满意;虎牙HR将员工抬出公司;瑞典禁用华为中兴5G设备
Improvement of maintenance mode of laravel8 update
华为在5G手机市场占据绝对优势,市调机构对小米的市占出现分歧
Solution of DEV-C + + unable to debug in Windows Environment
C++的那些事儿:从电饭煲到火箭,C++无处不在
LeanCloud 十月变化
AI weekly: employees are allowed to voluntarily reduce salary; company response: employees are happy and satisfied; tiger tooth HR takes employees out of the company; Sweden forbids Huawei ZTE 5g equi
Interpretation of deepmind's latest paper: the causal reasoning algorithm in discrete probability tree is proposed for the first time