shell命令awk使用例:

  1. 通过脚本打印出系统当前内存使用的百分比:
#!/bin/bash
[centos@centos shell]$ free -m
              total        used        free      shared  buff/cache   available
Mem:           1819         508         130          11        1180        1130
Swap:          2047         368        1679
[centos@centos shell]$ vim useCache.sh
echo "此脚本可以用来cha看当前系tong 内存使用百分比"
use=$(free -m | grep Mem: | awk '{print $3}')
total=$(free -m | grep Mem: | awk '{print $2}')
useper=$(expr $use \* 100 / $total)
echo "系tong当前内存使用百分比wei : "
echo ${useper}%
[centos@centos shell]$ chmod +x useCache.sh
[centos@centos shell]$ ./useCache.sh
此脚本可以用来cha看当前系tong 内存使用百分比
系tong当前内存使用百分比wei :
27%
[centos@centos shell]$

在这里插入图片描述
在这里插入图片描述

  1. 以空格为分隔,显示每行有多少字段
[centos@centos shell]$ vim file
aaaaaaaa bbbbb ccccc dddddd eeeee fffffff

bbbbbbbbbb aaaaa ccccccc ddddddddd eeeeeeeeee

ccccccc bbbbbbb eeeeee hhhhhhhhh

eeeeee ffffff

ffffff
[centos@centos shell]$ awk '{print NF}' file
6
0
5
0
4
0
3
0
2
0
1
  1. 以空格为分隔,查看文件中字段数大于4的行
[centos@centos shell]$ awk 'NF>4 {print}' file
aaaaaaaa bbbbb ccccc dddddd eeeee fffffff
bbbbbbbbbb aaaaa ccccccc ddddddddd eeeeeeeeee
[centos@centos shell]$
  1. 显示每一行的行号
[centos@centos shell_study]$ awk '{print NR, $0}' file
1 aaaaaaaa bbbbb ccccc dddddd eeeee fffffff
2
3 bbbbbbbbbb aaaaa ccccccc ddddddddd eeeeeeeeee
4
5 ccccccc bbbbbbb eeeeee hhhhhhhhh
6
7 fffff hhhhhhh yyyyyyyy
8
9 eeeeee ffffff
10
11 ffffff
[centos@centos shell_study]$ awk 'NR==5 {print}' file
ccccccc bbbbbbb eeeeee hhhhhhhhh
  1. 不显示第一行
[centos@centos shell_study]$ route -n | awk 'NR!=1{print}'
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.150.1   0.0.0.0         UG    100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.150.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
[centos@centos shell_study]$ route -n | awk 'NR>1{print}'
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.150.1   0.0.0.0         UG    100    0        0 ens33
192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0
192.168.150.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33
  1. 匹配文件中包含 root 的行
[centos@centos shell_study]$ awk -F: '/root/' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
  1. 不匹配文件中包含 root 的行
[centos@centos shell_study]$ awk -F: '!/root/' /etc/passwd
  1. 匹配文件中空行的行号
[centos@centos shell_study]$  awk '{if($0~/^$/)print NR}' file
2
4
6
8
10
  1. 不匹配文件中包含 root 的行
[centos@centos shell_study]$ awk -F: '!/root/' /etc/passwd
[centos@centos shell_study]$ awk -F: '{if($3>100)print "LARGE";else print "SMALL"}' /etc/passwd

在这里插入图片描述


GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:1 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐