当前位置:网站首页>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
边栏推荐
- 20 XR projects roadshows, nearly 20 capital institutions attended! We sincerely invite you to attend the 2020 qcomm XR eco Partner Conference
- Microservice - how to limit and fuse service current
- Key points of C language -- index article (let you fully understand indicators) | understand indicators from memory | complete analysis of indicators
- Do you really know how to use search engines?
- confd
- 9. Routingmesh service communication between clusters
- Let you have a deep understanding of gitlab CI / CD principle and process
- Application and principle of handlermethodargumentresolver
- idea 激活到 2089 失效
- 快進來!花幾分鐘看一下 ReentrantReadWriteLock 的原理!
猜你喜欢
站外推广的计划需要考虑哪些呢?
jenkins pipline stage 设置超时
【笔记】Error while loading PyV8 binary: exit code 1解决方法
How to solve the problem of blank page in Google Chrome browser
[note] error while loading pyv8 binary: exit code 1 solution
HMS Core推送服务,助力电商App开展精细化运营
HandlerMethodArgumentResolver使用和原理
confd
PHP security: the past and present of variables
条形码识别性能低,如何优化Dynamsoft Barcode Reader解码性能
随机推荐
【原創】ARM平臺記憶體和cache對xenomai實時性的影響
How to solve the problem of blank page in Google Chrome browser
jenkins pipline stage 设置超时
C enumerates the differences between permissions |, and |
Do you really know how to use search engines?
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?
Vue: Axios uses this pointer
使用LWA和Lync模拟外部测试无边缘单前端环境
How does LeadTools detect, read and write barcodes
一种超参数优化技术-Hyperopt
python3操作gitlab
How to use Gantt chart layers and filters
带你深入了解 GitLab CI/CD 原理及流程
PHP安全:变量的前世今生
New features of vue3
Introduction to Jenkins (2) declarative pipeline
廬山真面目之二微服務架構NGINX版本實現
Win7 how to quickly type CMD and get to the required directory
测试攻城狮必备技能点!一文带你解读DevOps下的测试技术
pc端与移动端适配解决方案之rem