当前位置:网站首页>【一天学awk】基础中的基础
【一天学awk】基础中的基础
2022-06-30 12:03:00 【程序员·小李】
1. awk是干什么的?
awk是linux环境中,类似于代码中的正则表达式的一种语法糖。
2. awk的语句组成

awk的语句主要分为三个部分:
一是BEGIN,这个部分主要是完成初始化工作,只执行一次,可有可无。
然后是重复执行的部分,repeat。这个部分针对每一个匹配项执行一次相应的操作。
最后是END部分,一般完成收尾操作,只执行一次,可有可无。
3. 尝试使用BEGIN
首先,我们创建一个文本,vi marks.txt
1) Learnfk Physics 80
2) Toolfk Maths 90
3) Chromefk Biology 87
4) Poemfk English 85
5) Nesfk History 89先来看看内容:

我们尝试使用awk语句,在marks.txt文本的前面加一句"This is the title."
awk 'BEGIN{printf "This is the title.\n"} {print}' marks.txt在文本的前面真的打印出了这个句子,说明BEGIN是首先执行的,并且只执行一次

4. 没有BEGIN的语句
我们可以直接执行重复的语句
awk '{print}' marks.txt
5. 将命令写入脚本
vi command.awk

然后使用awk -f command.awk marks.txt执行

效果和直接执行是一样的。
6. 命令指定变量
使用-v可以指定变量,例如
awk -v name=Bob 'BEGIN{printf "hello, %s\n", name}{print}' marks.txt
我们可以直接使用awk指定的变量。
边栏推荐
- Subtrate 源码追新导读-5月上旬: XCM 正式启用
- "Xiaodeng" user personal data management in operation and maintenance
- A new journey of the smart court, paperless office, escorting the green trial of the smart court
- Redis的配置文件及新数据类型
- MySql实现两个查询结果相除
- Edusoho enterprise training version intranet only deployment tutorial (to solve the problems of player, upload and background jam)
- Swagger2 automatically generates API documents
- Flutter 从零开始 005 图片及Icon
- Substrate 源码追新导读: 5月中旬: Uniques NFT模块和Nomination Pool
- 【目标跟踪】|pytracking 配置 win 编译prroi_pool.pyd
猜你喜欢

Solve the problem that the server cannot be connected via SSH during reinstallation

3D线光谱共焦传感器在半导体如何检测

MySQL composite query

Inner join and outer join of MySQL tables

Idea has a new artifact, a set of code to adapt to multiple terminals!

Redis cache problem

Analysis of the whole process of common tilt data processing in SuperMap idesktop

Lvgl widget styles

MySQL built-in functions

“\“id\“ contains an invalid value“
随机推荐
How to use the plug-in mechanism to gracefully encapsulate your request hook
药店管理系统
How difficult is data governance and data innovation?
MySql实现两个查询结果相除
Some commonly used hardware information of the server (constantly updated)
Use of polarplot function in MATLAB
Essay: Research on smart home scheme
杂文:自家的智能家居方案研究
Redis-缓存问题
不同类型的变量与零究竟是如何比较
MySQL中变量的定义和变量的赋值使用
Joplin implements style changes
90.(cesium篇)cesium高度监听事件
腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?
Map集合
两批次纯牛奶不合格?麦趣尔回应:正对产品大批量排查抽检
Shutter 007 input field from zero
Global capital market 101: Breit, one of the best investment targets for domestic high net worth people
Generate entity classes from SQL Server database tables through EF core framework
AGCO AI frontier promotion (6.30)