Unix/Linux 中shell命令 awk
shell命令awk使用例:
- 通过脚本打印出系统当前内存使用的百分比:
#!/bin/bash
[centos@centos shell]$ free -mtotal 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]$
- 以空格为分隔,显示每行有多少字段
[centos@centos shell]$ vim file
aaaaaaaa bbbbb ccccc dddddd eeeee fffffffbbbbbbbbbb aaaaa ccccccc ddddddddd eeeeeeeeeeccccccc bbbbbbb eeeeee hhhhhhhhheeeeee ffffffffffff
[centos@centos shell]$ awk '{print NF}' file
6
0
5
0
4
0
3
0
2
0
1
- 以空格为分隔,查看文件中字段数大于4的行
[centos@centos shell]$ awk 'NF>4 {print}' file
aaaaaaaa bbbbb ccccc dddddd eeeee fffffff
bbbbbbbbbb aaaaa ccccccc ddddddddd eeeeeeeeee
[centos@centos shell]$
- 显示每一行的行号
[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
- 不显示第一行
[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
- 匹配文件中包含 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
- 不匹配文件中包含 root 的行
[centos@centos shell_study]$ awk -F: '!/root/' /etc/passwd
- 匹配文件中空行的行号
[centos@centos shell_study]$ awk '{if($0~/^$/)print NR}' file
2
4
6
8
10
- 不匹配文件中包含 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
Unix/Linux 中shell命令 awk相关推荐
- Linux中shell命令的用法和技巧
使用Linux shell是我每天的基本工作,但我经常会忘记一些有用的shell命令和l技巧.当然,命令我能记住,但我不敢说能记得如何用它执行某个特定任务.于是,我开始在一个文本文件里记录这些用法,并 ...
- C++|Linux工作笔记-C++获取Linux中shell命令结果
目录 基本概念 代码与实例 基本概念 这里首先介绍一条Linux函数: ps -ef | awk '{print $2,$8,$9,$10}' 这个可以当前Linux系统运行的进程 另外一个知识点: ...
- 【Linux中shell命令】.sh文件种种操作
文章目录 Shell基础 Shell本身 Shell脚本 第一个Shell脚本 运行Shell脚本有三种方式 Shell bash和sh区别 .sh文件与.bash文件 Shell内置变量命令 eva ...
- linux中shell命令行缩进,Linux下几个实用的bash命令 | 旺旺知识库
一.扩展字段匹配 首先我们来看一个叫做扩展字段匹配的功能.这个选项允许你执行比标准Bash所提供的更复杂的字段匹配.例如,你可以定位除了后缀为.tmp之外的所有文件.扩展字段匹配功能可以通过shopt ...
- linux中shell命令之间与||符号
在linux中有时候我们会看到&& 跟|| 的符号 列如 : cp ~/aaa.txt /home/desktop/aaa.txt && echo " ...
- Unix/Linux 中的 shell 机制
Unix/Linux 中的 shell 机制 对于初次接触 Unix/Linux 系统的同学来说,Unix/Linux 系统与 Windows 系统最大的不同就是,操作 Unix/Linux 系统更多 ...
- Unix/Linux 中的 grep 命令
Unix/Linux 中的 grep 命令参数及示例用法 作用 语法 主要参数 示例命令 作用 Grep 筛选器在文件中搜索特定的字符模式,并显示包含该模式的所有行.在文件中搜索的模式称为 正则表达式 ...
- 八、Linux 常用 Shell 命令,控制台的快捷键以及 Shell 编程(中)
@Author : Runsen @Date:2020/9/11 文章是Runsen在Gitchat付费文章分享:Linux 常用 Shell 命令,控制台的快捷键以及 Shell 编程 顺便同步到C ...
- linux中安shell怎么传入参数,【linux】linux 下 shell命令 执行结果赋值给变量【两种方式】...
方法1:[通用方法] 使用Tab键上面的反引号 例子如下: find命令 模糊查询在/apps/swapping目录下 查找 文件名中包含swapping并且以.jar结尾的文件 使用反引号 引住命令 ...
最新文章
- 高德地图小蓝点_一会晴天一会下雨?夏日想要顺利出行 高德地图这些小功能最实用...
- 【深度学习笔记】Batch Normalization 以及其如何解决梯度消失问题
- 删除win10开始菜单中程序的目录
- 孙鑫VC学习笔记:第十九讲 动态链接库
- 免费英文文献下载攻略
- 计算机在我们日常生活中越来越重要英语,怎么翻译因特网在我们日常生活中起着越来越重要的作用用英语-网络的重要性用英语-英语-戈驴聪同学...
- JavaScript prototype原型实现继承
- java获取时分秒毫秒_java 中毫秒数转换成时分秒格式java中有什么方法可以把一个毫秒数格式化成”时:分:秒”...
- android怎么更换播放器,android 播放器切换 出错
- pcre c语言,pcre函数详细解析
- 淘宝二手闲鱼平台按关键词搜索闲鱼商品接口,item_search-按关键字搜索闲鱼商品接口接入说明方案
- WinEdt教程 表格,图像,公式,段落
- 第三章 决策树-隐形眼镜
- 【Day8.1】兰州牛肉拉面
- MATLAB怎么解方程解,怎么用Matlab解方程?
- 365打卡小程序搭建教程
- iOS 优化资源文件
- 赛门铁克召开2010财年中国合作伙伴高峰会
- 如何完全卸载VS2010(亲自体验过)
- 阿里笔试测评考什么?