当前位置:网站首页>awk实现类sql的join操作
awk实现类sql的join操作
2020-11-07 18:55:00 【程序猿欧文】
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 872 673 684 755 906 1007 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.........
版权声明
本文为[程序猿欧文]所创,转载请带上原文链接,感谢
https://my.oschina.net/mikeowen/blog/4707660
边栏推荐
- Idea activation to 2089 failure
- Key points of C language -- index article (let you fully understand indicators) | understand indicators from memory | complete analysis of indicators
- Exclusive interview with alicloud database of | 2020 PostgreSQL Asia Conference: Wang Xu
- Experiment one
- Web API interface design: swaggerui document / unified response format / unified exception handling / unified authority verification
- 嘉宾介绍|2020 PostgreSQL亚洲大会中文分论坛:潘娟
- After pulling four message queues into a group, they quarreled
- cubemx做stm32 USB开发
- jenkins pipline stage 设置超时
- Design pattern of facade and mediator
猜你喜欢
Three steps to understand Kerberos Protocol easily
Shanghai Pudong Development Bank, which frequently receives penalty tickets, has been cheated by hundreds of millions of yuan in loans, and lacks of internal control?
7.Swarm搭建集群
HMS core push service helps e-commerce app to carry out refined operation
CI / CD of gitlab continuous integrated development environment
Exclusive interview with Yue Caibo
Mac新手必备小技巧
10000! Ideal car recalls all defective cars: 97 accidents have occurred and losses will be expanded
How to use Gantt chart layers and filters
Jenkins pipline stage setting timeout
随机推荐
K-vim installation and the ycmd server shut down (restart with ': ycmrestartserver')
Implementation of nginx version of microservice architecture
python3操作Jenkins模块api
Application layer software development Godfather teaches you how to refactor, senior programmers must professional skills
把 4个消息队列都拉到一个群里后,他们吵起来了
Dynamsoft barcode reader v7.5!
Test the necessary skill points of siege lion! This article takes you to interpret the testing technology under Devops
ajax 载入html后不能执行其中的js解决方法
Jenkins入门(二)声明式流水线Jenkins Pipeline
条形码识别器Dynamsoft Barcode Reader v7.5全新上线!
Come on in! Take a few minutes to see how reentrantreadwritelock works!
Web API interface design: swaggerui document / unified response format / unified exception handling / unified authority verification
PHP security: the past and present of variables
失眠一个整晚上
快進來!花幾分鐘看一下 ReentrantReadWriteLock 的原理!
Exclusive interview with Yue Caibo
confd
DOM node operation
7.Swarm搭建集群
How does varhart xgantt represent working days on a calendar