首页
技术小册
AIGC
面试刷题
技术文章
MAGENTO
云计算
视频课程
源码下载
PDF书籍
「涨薪秘籍」
登录
注册
函数参数与返回值
传递变长参数
defer 和追踪
内置函数
递归函数
将函数作为参数
闭包
应用闭包:将函数作为返回值
使用闭包调试
计算函数执行时间
通过内存缓存来提升性能
声明和初始化
切片
For-range 结构
切片重组(reslice)
切片的复制与追加
字符串、数组和切片的应用
声明、初始化和 make
测试键值对是否存在及删除元素
for-range 的配套用法
map 类型的切片
map 的排序
将 map 的键值对调
标准库概述
regexp 包
锁和 sync 包
精密计算和 big 包
自定义包和可见性
为自定义包使用 godoc
使用 go install 安装自定义包
自定义包的目录结构、go install 和 go test
通过 Git 打包和安装
Go 的外部包和项目
在 Go 程序中使用外部库
结构体定义
使用工厂方法创建结构体实例
使用自定义包中的结构体
带标签的结构体
匿名字段和内嵌结构体
方法
类型的 String() 方法和格式化描述符
垃圾回收和 SetFinalizer
当前位置:
首页>>
技术小册>>
go编程权威指南(二)
小册名称:go编程权威指南(二)
**grep命令介绍** grep来自于英文词组“global search regular expression and print out the line”的缩写,意思是用于全面搜索的正则表达式,并将结果输出。人们通常会将grep命令与正则表达式搭配使用,参数作为搜索过程中的补充或对输出结果的筛选,命令模式十分灵活。 ------------ **常用参数:** | -i | 忽略大小写 | |----|--------------------------------| | -c | 只输出匹配行的数量 | | -l | 只列出符合匹配的文件名,不列出具体的匹配行 | | -n | 列出所有的匹配行,显示行号 | | -h | 查询多文件时不显示文件名 | | -s | 不显示不存在、没有匹配文本的错误信息 | | -v | 显示不包含匹配文本的所有行 | | -w | 匹配整词 | | -x | 匹配整行 | | -r | 递归搜索 | | -q | 禁止输出任何结果,已退出状态表示搜索是否成功 | | -b | 打印匹配行距文件头部的偏移量,以字节为单位 | | -o | 与-b结合使用,打印匹配的词据文件头部的偏移量,以字节为单位 | | -F | 匹配固定字符串的内容 | | -E | 支持扩展的正则表达式 | ------------ 参考实例 搜索某个文件中,包含某个关键词的内容: [root@linuxcool ~]# grep root /etc/passwd ```bash root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin ``` 搜索某个文件中,以某个关键词开头的内容: ```bash [root@linuxcool ~]# grep ^root /etc/passwd root:x:0:0:root:/root:/bin/bash ``` 搜索多个文件中,包含某个关键词的内容: ```bash u1@localhost:~$ sudo grep root /etc/passwd /etc/shadow [sudo] password for u1: /etc/passwd:root:x:0:0:root:/root:/bin/bash /etc/passwd:nm-openvpn:x:120:126:NetworkManager OpenVPN,,,:/var/lib/openvpn/chroot:/usr/sbin/nologin /etc/shadow:root:!:19207:0:99999:7::: ``` 搜索多个文件中,包含某个关键词的内容,不显示文件名称: [root@linuxcool ~]# grep -h root /etc/passwd /etc/shadow ```bash u1@localhost:~$ sudo grep -h root /etc/passwd /etc/shadow root:x:0:0:root:/root:/bin/bash nm-openvpn:x:120:126:NetworkManager OpenVPN,,,:/var/lib/openvpn/chroot:/usr/sbin/nologin root:!:19207:0:99999:7: ``` 输出在某个文件中,包含某个关键词行的数量: ```bash [root@linuxcool ~]# grep -c root /etc/passwd /etc/shadow /etc/passwd:2 /etc/shadow:1 ``` 搜索某个文件中,包含某个关键词位置的行号及内容: ```bash grep -n root /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 39:nm-openvpn:x:120:126:NetworkManager OpenVPN,,,:/var/lib/openvpn/chroot:/usr/sbin/nologin ``` 搜索某个文件中,不包含某个关键词的内容: ```bash u1@localhost:~$ grep -v nologin /etc/passwd root:x:0:0:root:/root:/bin/bash sync:x:4:65534:sync:/bin:/bin/sync tss:x:106:112:TPM software stack,,,:/var/lib/tpm:/bin/false whoopsie:x:117:124::/nonexistent:/bin/false speech-dispatcher:x:119:29:Speech Dispatcher,,,:/run/speech-dispatcher:/bin/false gnome-initial-setup:x:125:65534::/run/gnome-initial-setup/:/bin/false hplip:x:126:7:HPLIP system user,,,:/run/hplip:/bin/false gdm:x:127:133:Gnome Display Manager:/var/lib/gdm3:/bin/false u1:x:1000:1000:u1,,,:/home/u1:/bin/bash ``` 搜索当前工作目录中,包含某个关键词内容的文件,未找到则提示: ```bash u1@localhost:~$ grep -l root * grep: Desktop: Is a directory grep: Documents: Is a directory grep: Downloads: Is a directory grep: Music: Is a directory grep: Pictures: Is a directory grep: Public: Is a directory grep: snap: Is a directory grep: softs: Is a directory grep: Templates: Is a directory grep: Videos: Is a directory ``` 递归搜索,不仅搜索指定目录,还搜索其内子目录内是否有关键词文件: ```bash u1@localhost:/$ grep -srl root /etc /etc/xdg/Xwayland-session.d/00-at-spi /etc/brltty.conf /etc/initramfs-tools/initramfs.conf /etc/X11/Xwrapper.config /etc/X11/Xreset.d/README /etc/logrotate.d/alternatives ………………省略部分输出信息……………… ``` 判断某个文件中,是否包含某个关键词,通过返回状态值输出结果(0为包含,1为不包含),方便在Shell脚本中判断和调用: ```bash [root@linuxcool ~]# grep -q root /etc/passwd [root@linuxcool ~]# echo $? 0 ``` 搜索某个文件中,空行的数量: ```bash u1@localhost:~/Documents$ grep -c ^$ test.txt 7 ```
上一篇:
查看文件内容
下一篇:
curl命令-文件传输工具
该分类下的相关小册推荐:
GO面试指南
Go开发基础入门
深入解析go语言
Golang修炼指南
深入浅出Go语言核心编程(二)
Golang并发编程实战
Go进阶之分布式爬虫实战
Go Web编程(中)
深入浅出Go语言核心编程(四)
Go Web编程(下)
深入浅出Go语言核心编程(八)
go编程权威指南(三)