当前位置:网站首页>Shell script, awk uses if, for process control
Shell script, awk uses if, for process control
2022-07-05 21:46:00 【Excellent dengzonglei】
Shell Script ,awk Process control , Support for,if etc.
One 、 Single branch if Judge
1、awk ‘{ Instructions }’ dir
awk -F: ‘BEGIN{x=0} {if($3>=1000){i++}}END{print i}’ /etc/passwd
In the statistical file uid Greater than or equal to 1000 Quantity of content .
2、awk -F: ‘{if($1==“root”){print $1,$3}}’ /etc/passwd
awk The style is similar to the programming language , Constants use double quotation marks , You can't use single quotes or backquotes 
Two 、 Multiple branches if Judge
1、 Double branch if…else
awk -F: ‘{if($3>=1000){i++}else{j++}} END{print i,j}’ /etc/passwd
Judge uid1000 The number of users and 1000 The number of users below 
2、 Multiple branches ,if…else if…else
awk -F: ‘{if($30){print $1}else if($31000){print $1}else{print $7}}’ /etc/passwd
3、 ... and 、for loop ,awk Of for use c The grammatical format of a language
awk ‘BEGIN{for(i=1;i<=5;++i){print i}}{print}’ /etc/passwd
Print numbers before printing user information 1-5
Four 、awk Arrays and Applications
1、 Define an array
Format : Array name [ Subscript ]= Element value
awk ‘BEGIN{name[0]=“TOM”;name[1]=“JERRY”;print name[0],name[1]}’
Be careful print The following variables are used , Separate , Will print separately with spaces on the same line , If you use a semicolon and write two print, Two lines of content will be printed separately .
2、 Call array
Array name [ Subscript ]
3、 Traversal array
for( Variable name in Array name ){print Array name [ Variable ]}
The definition of an array cannot be name=“1234”,print name[1], This is used in awk There are grammatical errors in 
Example :
1、awk ‘BEGIN{name[0]=“TOM”;name[1]=“JERRY”;print name[0],name[1]}’
Print... On the screen TOM,JERRY
2、awk ‘{array[$1]++}END{for(i in array){print i,array[i]}}’ dir
Print the number of repetitions of the contents of the first column in the specified file 
边栏推荐
- SecureCRT使用提示
- Environment configuration problem record
- Clickhouse copy paste multi line SQL statement error
- Matlab | app designer · I used Matlab to make a real-time editor of latex formula
- 2022-07-03-CKA-粉丝反馈最新情况
- [daily training -- Tencent select 50] 89 Gray code (only after seeing the solution of the problem)
- regular expression
- Poj3414广泛搜索
- How can Huawei online match improve the success rate of player matching
- 854. 相似度为 K 的字符串 BFS
猜你喜欢

2022-07-03-cka- latest feedback from fans

張麗俊:穿透不確定性要靠四個“不變”

MQ----activeMq

深信服X计划-网络协议基础 DNS

Analysis and test of ModbusRTU communication protocol

Alibaba cloud award winning experience: build a highly available system with polardb-x

MMAP学习

DBeaver同时执行多条insert into报错处理
![Longest swing sequence [greedy practice]](/img/e1/70dc21b924232c7e5e3da023a4bed2.png)
Longest swing sequence [greedy practice]

What should I do to prepare for the interview algorithm position during school recruitment?
随机推荐
Haas506 2.0 development tutorial - Alibaba cloud OTA - PAC firmware upgrade (only supports versions above 2.2)
About the writing method of SQL field "this includes" and "included in" strings
Opérations de lecture et d'écriture pour easyexcel
让开发效率飞速提升的跨端方案
力扣------经营摩天轮的最大利润
ICMP 介绍
EBS Oracle 11g 克隆步骤(单节点)
Kingbasees v8r3 data security case - audit record clearing case
Cross end solutions to improve development efficiency
PostGIS installation geographic information extension
Parker driver maintenance COMPAX controller maintenance cpx0200h
Making global exception handling classes with aspect
Selenium finds the contents of B or P Tags
How to organize an actual attack and defense drill
datagrid直接编辑保存“设计缺陷”
MMAP learning
Robot framework setting variables
Emotional analysis of wechat chat records on Valentine's day based on Text Mining
Summary of El and JSTL precautions
R language learning notes