Linux learning awk basic programming

little bottle
Release: 2019-04-18 16:11:26
forward
3463 people have browsed it

 awk是一个使用非常广泛的文本处理工具,是功能最强大的数据处理引擎之一,可以进行样式的装入,流控制,数学运算符,进程控制语句甚至于内置变量和函数,下来就来详细介绍它的基本使用方示,和大家一起分享一下!

1.awk每次读一行数据,如果设置了-F选项,是在这一行使用分隔符分,$0是全部

2.awk由模式和动作组成

3.条件判断模式{动作}


a.txt
aaa
bbb
aaa
ccc
ddd
cat a.txt |awk '$1=="aaa"{print "=="$1}'
Copy after login

4./正则模式/{动作}

可以有多个模式动作

5.条件操作符,~匹配正则 !~不匹配


cat a.txt |awk '{if ($1=="aaa") print $1;else print "==="$1}'
Copy after login

6.特殊的模式(BEGIN,END) , 都只执行一次 , 不像前面的模式 , 每一行都会执行 , 用于初始化和最终结果的打印等


cat a.txt |awk '{arr[$1]+=1}END{for(row in arr){print row" 出现次数 "arr[row]}}'
Copy after login

配合数组统计出现次数

 推荐教程:Linux视频教程

The above is the detailed content of Linux learning awk basic programming. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template