1a. choose rows where column 3 is larger than column 5:
awk '3>5' input.txt > output.txt
1b. calculate the sum of column 2 and 3 and put it at the end of a row:
awk '{print 0,2+$3}' input.txt
or replace the first column:
awk '{1=2+$3;print}' input.txt
2. show rows between 20th and 80th (better head):
awk 'NR>=20&&NR<=80' input.txt > output.txt
3. calculate the average of column 2:
awk '{x+=$2}END{print x/NR}' input.txt
4. extract column 2,4,5 (cut):
awk '{print 2,4,$5}' input.txt > output.txt
or
awk 'BEGIN{OFS="\t"}{print 2,4,$5}' input.txt
5. (more complicated) join two files on column 1 (better join):
awk 'BEGIN{while((getline<"file1.txt")>0)l[1]=0}$1 in l{print 0"\t"l[1]}' file2.txt > output.txt
6. count number of occurrence of column 2 (uniq -c):
awk '{l[$2]++}END{for (x in l) print x,l[x]}' input.txt
7. apply "uniq" on column 2, only printing the first occurence (uniq):
awk '!($2 in l){print;l[$2]=1}' input.txt
8. count different words (wc):
awk '{for(i=1;i!=NF;++i)c[$i]++}END{for (x in c) print x,c[x]}' input.txt
9. deal with simple CSV:
awk -F, '{print 1,2}'
10. regex (egrep):
awk '/^test[0-9]+/' input.txt
11. substitution (sed is simpler):
awk 'BEGIN{OFS="\t"}{sub(/test/, "no", $0);print}' input.txt
awk 简单用法小结
tanhao2013@foxmail.com || http://weibo.com/buttonwood
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何在 .NET 中 使用 ANTLR4
· 后端思维之高并发处理方案
· 理解Rust引用及其生命周期标识(下)
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 想让你多爱自己一些的开源计时器
· Cursor预测程序员行业倒计时:CTO应做好50%裁员计划
· 大模型 Token 究竟是啥:图解大模型Token
· 用99元买的服务器搭一套CI/CD系统
· 如何在 .NET 中 使用 ANTLR4