当前位置:网站首页>Grep工具
Grep工具
2022-06-29 21:50:00 【51CTO】
本节课程目标
●了解shell中的通配符
●熟悉grep、cut、sort等小工具和shell中的通配符的使用
一、文本处理工具
1. grep工具
grep是**行**过滤工具;用于根据关键字进行行过滤
语法和选项
语法
常见选项:
OPTIONS:
-i: 不区分大小写
-v: 查找不包含指定内容的行,反向选择
-w: 按单词搜索
-o: 打印匹配关键字
-c: 统计匹配到的行数
-n: 显示行号
-r: 逐层遍历目录查找
-A: 显示匹配行及后面多少行
-B: 显示匹配行及前面多少行
-C: 显示匹配行前后多少行
-l:只列出匹配的文件名
-L:列出不匹配的文件名
-e: 使用正则匹配
-E:使用扩展正则匹配
^key:以关键字开头
key$:以关键字结尾
^$:匹配空行
--color=auto :可以将找到的关键词部分加上颜色的显示
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
颜色显示(别名设置):
举例说明:
说明:不要直接使用/etc/passwd文件,将其拷贝到/tmp下做实验!
# grep -i root passwd 忽略大小写匹配包含root的行
# grep -w ftp passwd 精确匹配ftp单词
# grep -w hello passwd 精确匹配hello单词;自己添加包含hello的行到文件
# grep -wo ftp passwd 打印匹配到的关键字ftp
# grep -n root passwd 打印匹配到root关键字的行好
# grep -ni root passwd 忽略大小写匹配统计包含关键字root的行
# grep -nic root passwd 忽略大小写匹配统计包含关键字root的行数
# grep -i ^root passwd 忽略大小写匹配以root开头的行
# grep bash$ passwd 匹配以bash结尾的行
# grep -n ^$ passwd 匹配空行并打印行号
# grep ^# /etc/vsftpd/vsftpd.conf 匹配以#号开头的行
# grep -v ^# /etc/vsftpd/vsftpd.conf 匹配不以#号开头的行
# grep -A 5 mail passwd 匹配包含mail关键字及其后5行
# grep -B 5 mail passwd 匹配包含mail关键字及其前5行
# grep -C 5 mail passwd 匹配包含mail关键字及其前后5行
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
2. cut工具
cut是**列**截取工具,用于列的截取
语法和选项
语法:
常见选项:
举例说明:
课堂练习: 用小工具列出你当系统的运行级别。5/3
- 如何查看系统运行级别
- 命令
runlevel - 文件
/etc/inittab
- 如何过滤运行级别
runlevel |cut -c3
runlevel | cut -d ' ' -f2
grep -v '^#' /etc/inittab | cut -d: -f2
grep '^id' /etc/inittab |cut -d: -f2
grep "initdefault:$" /etc/inittab | cut -c4
grep -v ^# /etc/inittab |cut -c4
grep 'id:' /etc/inittab |cut -d: -f2
cut -d':' -f2 /etc/inittab |grep -v ^#
cut -c4 /etc/inittab |tail -1
cut -d: -f2 /etc/inittab |tail -1
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
边栏推荐
- 文件操作的底层原理(文件描述符与缓冲区)
- The correct method for Navicat to connect to mysql8.0 (valid for personal testing)
- Numpy's research imitation 1
- 生产环境AIX小机报错B6267342问题处理
- Layer 3 loop brought by route Summary - solution experiment
- Taro2.* applet configuration sharing wechat circle of friends
- Alibaba keyword search commodity API interface (item_search- commodity search interface by keyword), Alibaba Search API interface
- DevCloud加持下的青软,让教育“智”上云端
- Reflections on remote sensing image interpretation
- C. Most Similar Words
猜你喜欢

Deep learning remote sensing data set

Reading notes on how to connect the network - Web server request and response (V)

2022 openvino DevCon unveils secrets! Intel and many partners deepen the construction of developer ecology and release the innovation potential of AI industry

从第三次技术革命看企业应用三大开发趋势

夏日彩虹来下饭

软件测试方法和技术知识点有哪些?

为什么要同时重写hashcode和equals方法之简单理解
![[cloud native] use of Nacos taskmanager task management](/img/ad/24bdd4572ef9990238913cb7cd16f8.png)
[cloud native] use of Nacos taskmanager task management

Shangsilicon Valley real-time data warehouse project (Alibaba cloud real-time data warehouse)

ASP动态创建表格 Table
随机推荐
Numpy's research imitation 1
The soft youth under the blessing of devcloud makes education "smart" in the cloud
The solution to the "undefined symbol: \u cxa\throw\bad\array\new\u length, version qt\u 5" error reported by the Kirin system startup application
Change detection and batch update
Detailed description of gaussdb (DWS) complex and diverse resource load management methods
澳洲要求PVC 塑料片符合AS/NZS 1530.3 火焰蔓延指数为0吗?
5-minute quick start pytest testing framework
阶段性总结与思考
Final training simple address book c language
leetcode:91. 解码方法【dfs + 记忆化】
从第三次技术革命看企业应用三大开发趋势
Cloud native database query optimization - statistics and row count estimation
ASP. Net cross page submission (button control page redirection)
Use of golang gopsutil Library: process and system resource monitoring (CPU, memory, disk, etc.)
Aleph farms hired a supervisor of regulatory affairs to prepare for global commercialization in advance
Taro2.* applet configuration sharing wechat circle of friends
The child component of a single data flow modifies the value of the parent component
Graduation summary of construction practice camp
Sophon CE community edition goes online, and free get is a lightweight, easy-to-use, efficient and intelligent data analysis tool
5分钟快速上手 pytest 测试框架