博客
关于我
无基础学Linux(11)——grep、awk、sed
阅读量:657 次
发布时间:2019-03-15

本文共 824 字,大约阅读时间需要 2 分钟。

Linux三剑客

一、

grep是一种强大的文本搜索工具,它能使用特定模式匹配搜索文本,并默认输出匹配行。用于查找文件里符合条件的字符串(信息筛选)

二、

awk是一个优良的文本处理工具,Linux及Unix环境中现有的功能最强大的数据处理引擎之一。

awk的基本语法结构

在这里插入图片描述
![在这里插入图片描述](https://img-blog.csdnimg.cn/20210506113114997.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3F5dF83MjI=,size_16,color_FFFFFF,t_70

last | grep -v "^$" | grep -v "wtmp" | head -3 | awk 'BEGIN {printf("%s\n","start")} {print}'

last 显示信息

grep -v "^$" | grep -v "wtmp" 把空行和不需要的内容筛掉
head -3只取前三行
awk 'BEGIN {printf("%s\n","start")} {print}'输出,且在最开始打印start字样

有头有尾:

在这里插入图片描述
只打印出时间:
在这里插入图片描述
打印出使用时长
在这里插入图片描述

三、

sed功能同awk类似,差别在于,sed简单,利用脚本来处理文本文件,对列处理的功能要差一些,awk的功能复杂,对列处理的功能比较强大。

在这里插入图片描述cat a.log | sed 's/abc/123/g'
在这里插入图片描述
以上后面不加g也可sed 's/abc/456/' a.log
在这里插入图片描述
删除其中第一行和第二行sed '1,2d' a.log
在这里插入图片描述
删掉有added参数的一行:sed '/added/d' a.log
在这里插入图片描述
单行增加以及单行显示
在这里插入图片描述
将行首都转换成$符号
在这里插入图片描述
都删掉:
在这里插入图片描述
在vim中有同等功效的指令:g全局替换
在这里插入图片描述在这里插入图片描述

转载地址:http://nxdmz.baihongyu.com/

你可能感兴趣的文章
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
node
查看>>
node exporter完整版
查看>>
node HelloWorld入门篇
查看>>
Node JS: < 一> 初识Node JS
查看>>
Node JS: < 二> Node JS例子解析
查看>>
Node Sass does not yet support your current environment: Linux 64-bit with Unsupported runtime(93)解决
查看>>
Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime(72)
查看>>
Node 裁切图片的方法
查看>>
node+express+mysql 实现登陆注册
查看>>
Node+Express连接mysql实现增删改查
查看>>