当前位置:网站首页>Awk syntax-03-awk expressions (if statements, while loops, for loops), execute shell commands in awk
Awk syntax-03-awk expressions (if statements, while loops, for loops), execute shell commands in awk
2022-08-08 04:07:00 【DevOps Xuande Gong】
1. 条件表达式
语法
awk '{print (条件)?Satisfy the condition and give the value:A value is given if the condition is not met}' 文件名
完整示例
- 创建myAwk.awk文件
#!/bin/awk -f
BEGIN{
FS=":";
line="============================================";
print "Type\t\tName\t\tId\n",line
}
{
print ($3!=0&&$3<500)?"系统用户:\t"$1"\t\t"$3:"普通用户:\t"$1"\t\t"$3;
}
- 执行
[[email protected] ~]# ./myAwk.awk /etc/passwd
Type Name Id
============================================
普通用户: root 0
系统用户: bin 1
系统用户: daemon 2
系统用户: adm 3
系统用户: lp 4
系统用户: sync 5
……
2. if 语句
语法
if(条件){
执行内容
}else if(){
执行内容
}else{
执行内容
}
示例
- 创建myAwk.awk 文件如下
#!/bin/awk -f
BEGIN{
FS=":";
line="============================================";
printf "|%-20s|%-20s|%-20s\n%s\n","Type","Name","Id",line
}
{
if($3==0){
printf "|%-20s|%-20s|%-20s\n","root_user",$1,$3
}else if($3>=1000){
printf "|%-20s|%-20s|%-20s\n", "ord_user",$1,$3
}else{
printf "|%-20s|%-20s|%-20s\n", "sys_user",$1,$3
}
}
- 执行
[[email protected] ~]# ./myAwk.awk /etc/passwd
|Type |Name |Id
============================================
|root_user |root |0
|sys_user |bin |1
|sys_user |daemon |2
|sys_user |adm |3
|sys_user |lp |4
|sys_user |sync |5
|sys_user |shutdown |6
|sys_user |halt |7
|sys_user |mail |8
|sys_user |operator |11
|sys_user |games |12
|sys_user |ftp |14
|sys_user |nobody |99
|sys_user |systemd-network |192
|sys_user |dbus |81
|sys_user |polkitd |999
|sys_user |sshd |74
|sys_user |postfix |89
|sys_user |chrony |998
|ord_user |cloud_user |1000
|ord_user |liubei |1001
|ord_user |guanyu |1002
|ord_user |guanping |1003
3. while循环
语法
while(循环条件){
执行内容
}
示例
每行打印10遍
- 创建myAwk.awk 文件如下
#!/bin/awk -f
BEGIN{
FS=":"
}
{
i=0
while(i<=10){
print $1,$3
i++
}
}
- 执行
[[email protected] ~]# ./myAwk.awk /etc/passwd
root 0
root 0
root 0
root 0
root 0
root 0
root 0
root 0
root 0
root 0
root 0
bin 1
bin 1
bin 1
bin 1
bin 1
bin 1
bin 1
bin 1
bin 1
bin 1
bin 1
……
4. for 循环
语法
for(i=x;i<=n;i++){
执行内容
}
示例
- 创建myAwk.awk 文件如下
每行打印10次
#!/bin/awk -f
BEGIN{
FS=":"
}
{
for(i=0;i<=10;i++){
print $1,$3
}
}
- 执行
[[email protected] ~]# ./myAwk.awk /etc/passwd
root 0
root 0
root 0
root 0
root 0
root 0
root 0
root 0
root 0
root 0
root 0
bin 1
bin 1
bin 1
bin 1
bin 1
bin 1
bin 1
bin 1
bin 1
bin 1
bin 1
……
5. 执行shell命令
语法
{
system("内容1 " 内容2 )}
说明:
- 引号中的部分,It will be passed to the system intact
- part outside quotation marks,会经过awkAfter processing, it is sent to the system(Therefore variables should be written outside the quotation marks)
示例
to each ordinary user's home directory
用户名.txtWrite the current time in the file
- 创建myAwk.awk文件
#!/bin/awk -f
BEGIN{
FS=":"
}
{
if($3>1000){
system("echo `date` > /home/"$1"/"$1".txt" )
}
}
- 执行
[[email protected] ~]# ./myAwk.awk /etc/passwd
[[email protected] ~]# cat /home/liubei/liubei.txt
2022年 07月 31日 星期日 17:16:38 CST
[[email protected] ~]# cat /home/guanyu/guanyu.txt
2022年 07月 31日 星期日 17:16:38 CST

边栏推荐
- New retail project and offline warehouse core interview,, 220807,,
- Knowledge of DisplayPort-DP interface
- MySQL从入门到入土【20W字收藏篇】
- 第4周 一步步搭建多层神经网络以及应用(1 & 2)
- 新零售项目及离线数仓核心面试,,220807,,
- VSCode打开 C(嵌入式) 工程的一些记录
- An egg - Nodemailer - qq email verification code development configuration
- Simulate login - add cookies, use postmanget to request web page data
- leetcode: 455. 分发饼干
- Vulfocus Shooting Range Scenario Mode - Intranet Dead End
猜你喜欢

Hangzhou Electric Multi-School 6 1009. Map

国内最主流的5大项目工时管理系统

使用z-file和七牛云对象存储构建个人网盘

Inside outside l think MindSpore AI framework, heavy industry gathering, huawei big extraordinary path of the model

Young freshmen who yearn for open source | The guide to avoiding pits from open source to employment is here!

Monitoring tool Prometheus and project summary, 220805,,

Bluetooth att gatt agreement

数据库篇复习篇

Risk control strategy must be learned | This method of mining rules with decision trees

Vulfocus Shooting Range Scenario Mode - Intranet Dead End
随机推荐
Vulfocus Shooting Range Scenario Mode - Intranet Dead End
【opencv】opencv开发包简介
New retail project and offline warehouse core interview,, 220807,,
How to avoid bugs as much as possible
32. Do you know how Redis strings are implemented?
[Code Analysis] Graph small sample anomaly detection method: GDN: Few-shot Network Anomaly Detection via Cross-network Meta-learning
蓝牙 att gatt 协议
Hangzhou Electric Multi-School 6 1009. Map
MySQL from entry to entry [20W word collection]
数据在内存如何分布的?
以0为底或以1为底对图片迭代次数的影响
Build a personal network disk using z-file and Qiniu cloud object storage
关于 globalThis is not defined 报错问题
Optional中orElse和orElseGet的区别
【图基础】如何定义异质图上的小样本学习:Heterogeneous Graph Few-Shot Learning
leetcode 112.路经总和 递归
机器学习笔记:学习率预热 warmup
【模板引擎】velocity
egg-session 将数据存储到redis
JS 怎么使用十六进制保存100位状态的问题