grep/字符/次数匹配/锚定符/小大括号/wc/tr/cut/sort/uniq
grep:正则表达式,文本过滤工具,能够实现以指定的"模式(Pattern)"逐行搜索文件中的内容,并将匹配到的行显示出来.
模式:是由正则表达式的元字符,其他字符组合起来的匹配字符。
每一类正则表达式本身的表达式是需要用户自己去写的,但表达式的元字符都有着固定的或者特定的意义,我们可以根据自己的需要去理解或者组合字符,生成我们需要的模式
| -v:显示不被模式匹配到的行,invert-match
-i:在做模式匹配的时候不区分大小写ignore-case -o:只显示匹配到的串,而非默认显示匹配到的行,only-matching -A 数字:A=after,显示到匹配到的行后,还显示每一个匹配项下面*行的内容 -B 数字:before,前*行 -C 数字:context,上下文*行 -E:扩展的正则表达式 |
例子:
| [root@lbg test]# grep -o 'root' /etc/passwd --只显示字段,不显示整行。
[root@lbg test]# grep -A 1 'root' /etc/passwd --显示该行和下一行内容 [root@lbg test]# grep -B 2 'root' /etc/passwd --显示该行和其前两行内容。 |
1.字符匹配( . [] [^] [:space:] [:punct:])
| . 匹配任意单个字符(制表符,空格,标点,字母,数 字,其他符号)
[] 匹配指定范围内的任意单个字符 [^] 指定范围外的单个字符 [:space:] 匹配空白字符,包括空格,tab, [:punct:] 标点符号(用法同空白字符) |
例子:
| [root@lbg test]# grep 'r[oa]t' /etc/passwd operator:x:11:0:operator:/root:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin |
| * (贪婪模式) 做次数匹配,匹配*前面的字符0,1或多次,只表示次数,不表示字符
? 只做次数匹配,0或1次,写法是: \? {m,n} 最少m次,最多n次 写法为: \{m,n\} 用{m,n}时可以没有上限,但下限一定要有. 且 ?和{}都要加\. |
例子:
| [root@lbg test]# cat 1 ab a b a b a b [root@lbg test]# grep 'a b' 1 --匹配1个空格 a b [root@lbg test]# grep 'a *b' 1 --匹配0及以上的空格 ab a b a b a b a b [root@lbg test]# grep 'a \?b' 1 --匹配0或1个空格 ab a b [root@lbg test]# grep 'a \{2,3\}b' 1 ---匹配2或3个空格 a b a b [root@lbg test]# grep 'a \{2,\}b' 1 --匹配2个及以上空格 a b a b a b |
| \< 锚定词首 ####找到以r..t开头的内容 \
\> 锚定词尾 ### r..t\> ^ 脱字符 行首锚定 $ 行尾锚定 ##### root$ -->必须以root结尾 |
例子:
| [root@lbg test]# grep '\
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@lbg test]# grep 'ot\>' /etc/passwd ---ot结尾的单词
root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin setroubleshoot:x:993:988::/var/lib/setroubleshoot:/sbin/nologin [root@lbg test]# grep '^ro' /etc/passwd --以ro开头的行 root:x:0:0:root:/root:/bin/bash [root@lbg test]# grep 'bash$' /etc/passwd --以bash结尾的行 |
| [root@lbg test]# ls ---无文件 [root@lbg test]# touch a --创建文件a [root@lbg test]# cp /test/a{,1} --将a文件复制,并在其原路径下重命名为a1. [root@lbg test]# ls a a1 |
| wc [options]... file_name
-l 仅显示行数(line) -w 仅显示单词数(word) -c 仅显示字节数(char) 单独的wc显示的信息依次是:行数 单词数 字节数 文件名 |
例子:
| [root@lbg test]# cat a how do you do how do you do [root@lbg test]# wc a ---显示4种信息 2 8 28 a [root@lbg test]# wc -l a --显示行数 2 a [root@lbg test]# ls /test a b c d [root@lbg test]# ls /test | wc -l ----使用wc统计目录下文件的个数 4 |
| tr:转换字符或者删除字符(tr -d '字符集合' --delete )
tr '集合1' '集合2' --将集合1里的内容按集合2对应位置的内容替换显示出来。 |
例子:
| [root@lbg test]# echo 'abcdabcd' |tr 'ac' '13' --两边集合一一对应的情况 1b3d1b3d [root@lbg test]# echo 'abcdabcd' |tr 'acd' '13' --集合1多于集合2的情况 1b331b33 [root@lbg test]# echo 'abcdabcd' |tr 'acd' '1234' --集合1少于集合2的情况 1b231b23 [root@lbg test]# echo 'abcdabcd' |tr 'a-z' 'A-Z' --大小写转换 ABCDABCD [root@lbg test]# echo 'abcdabcd' |tr -d 'ab' ---删除字符 cdcd |
| -c :按照字符数量切 (characters)
-d :按指定分隔符切(delimiter 定界符) -f:指定要显示的字段 (file) 单个数字-->一个字段 逗号分隔的多个数字-->指定多个离散字段 - -->连续字段,3-5表示3到5字段 |
例子:
| [root@lbg test]# echo 'there are many dogs'|cut -d ' ' -f 2 --按空格切 are [root@lbg test]# echo 'there are many dogs'|cut -d ' ' -f 2,4 --第2个和第4个 are dogs [root@lbg test]# echo 'there are many dogs'|cut -d ' ' -f 2-4 --2到4个 are many dogs [root@lbg test]# echo 'there are many dogs'|cut -d m -f 1 --以m切割 there are [root@lbg test]# echo 'there are many dogs'|cut -d m -f 2 --以m切割 any dogs [root@lbg test]# echo 'how do you do'|cut -c 2-5 ---取第2到第5个字符 ow d |
| -f:忽略字符大小写(ignore-case)
-n:对数值进行排序 //Sort 默认字符排序 加,-n 数字排序. -r : 逆序输出 -t:指定分隔符 -k:基于哪个字段进行排序 (key) -u:uniq,重复的行只显示一行 (unique) |
例子:
| [root@lbg test]# cut -d : -f 3 /etc/passwd |sort -n [root@lbg test]# cat a 234 123 234 [root@lbg test]# sort -u a 123 234 [root@lbg test]# sort -n a 123 234 234 [root@lbg test]# sort -n -u a 123 234 |
9.uniq:去重复行。
| -c:统计每一行出现的次数(count)
-d:仅显示重复过的行 -u:仅显示未重复行 注意uniq去重,认定是连续的才是重复的.不是连续的不会去重. |
例子:
| [root@lbg test]# cat a 234 234 123 234 [root@lbg test]# uniq a 234 123 234 [root@lbg test]# sort a | uniq -c 1 123 3 234 [root@lbg test]# sort -u -n a 123 234 |
10.小括号向前引用功能
\1--引用从左到右的第一个括号内范围.
\2--引用从左到右的第二个括号内范围.
例子:
| [root@lbg test]# cat a he love his lover she like her liker [root@lbg test]# grep 'l..e.*e' a he love his lover she like her liker [root@lbg test]# grep '\(l..e\).*\1' a ---与命令grep 'l..e.*e' a相同 he love his lover she like her liker [root@lbg test]# grep '\(l\(..\)e\).*\1' a ---与命令grep 'l..e.*e' a相同 he love his lover she like her liker [root@lbg test]# grep 'l..e.*l..' a he love his lover she like her liker [root@lbg test]# grep '\(l\(..\)e\).*\2' a --与grep 'l..e.*l..' a 相同 he love his lover she like her liker |
grep/字符/次数匹配/锚定符/小大括号/wc/tr/cut/sort/uniq的更多相关文章
- 线上问题debug过程(cat,grep,tr,awk,sort,uniq,comm等工具的综合使用)
问题:发现线上到货单的数量,小于实际到货的数量. 怀疑一些隐藏的条件,将部分唯一码进行了过滤,导致数量变少. 开展了如下的跟踪流程: 1.找到其中一个明细的唯一码 grep 6180e-4b09f p ...
- php正则:匹配(),{},[]小括号,大括号,中括号里面的内容
php正则:匹配(),{},[]小括号,大括号,中括号里面的内容 比如有一段字符: $s='60c8 {"code":"200","message&q ...
- grep中正则匹配的使用
如要匹配Computer或computer两个单词,可做如下操作: [Cc]mputer “.”允许匹配ASCII集中任意字符,或为字母,或为数字. 使用\{\}匹配模式结果出现的次数 匹配字母A出现 ...
- grep 常用正则匹配
1.或操作 grep -E '123|abc' filename // 找出文件(filename)中包含123或者包含abc的行 egrep '123|abc' filename // 用egrep ...
- Linux篇---Grep和正则匹配
一.前述 Linux中正则匹配查找比较常用,所以分享一篇关于正则匹配和Grep结合的文章. 二.匹配规则 匹配操作符: \ 转义字符. ...
- 第11.11节 Python正则表达式的指定重复次数匹配模式及元字符”{}”功能介绍
在<第11.8节 Pytho正则表达式的重复匹配模式及元字符"?". "". "+"功能介绍>和<第11.10节 Pyth ...
- Python第二天 变量 运算符与表达式 input()与raw_input()区别 字符编码 python转义符 字符串格式化 format函数字符串格式化 帮助
Python第二天 变量 运算符与表达式 input()与raw_input()区别 字符编码 python转义符 字符串格式化 format函数字符串格式化 帮助 目录 Pychar ...
- grep是模糊匹配
1. 我:我用的ps -nat|grep -i "80"|wc -l命令 我:解释详细点,,龙哥,对于我这种菜鸟:也是模糊匹配 :你用 grep "80" 会匹 ...
- 《c程序设计语言》读书笔记--每行一个单词打印输入的字符,除去空符
#include <stdio.h> int main() { int c; while((c = getchar()) != EOF) { if(c != '\n' && ...
随机推荐
- 深入理解RabbitMQ中的prefetch_count参数
前提 在某一次用户标签服务中大量用到异步流程,使用了RabbitMQ进行解耦.其中,为了提高消费者的处理效率针对了不同节点任务的消费者线程数和prefetch_count参数都做了调整和测试,得到一个 ...
- ASP.NET Core 3.1 Razor 视图预编译、动态编译
1.安装NuGet包 Install-Package Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation 2.Startup.cs 配置 public ...
- python 安装matplotlib
下载minianaconda 安装 进入cmd下 conda create --name python37 python=3.7 创建python3.7环境 conda activate pytho ...
- PHPStorm注释缩进问题
以下是让强迫症很难受的注释格式 以下几步即可解决强迫症的烦恼 1.点击File 2.点击setting 3.按照以下几步走 4.按照以下几步走 5.最后效果如下
- Python之数据类型总结
1.字符串 2.数字 3.列表 4.元组 5.字典 可变 or 不可变 1:可变:列表.字典 2:不可变:字符串,数字,元组 访问顺序 1.直接访问:数字 2.顺序访问:字符串,列表,元组 3.映射访 ...
- Linux运维学习第一周记
1 当年白岳伴清游, 2 江石台空一苇浮. 3 缥渺临风闻郢曲, 4 殷勤歧路看吴钩. 老气横秋方知世间沧桑! 以前一直忙,没有时间沉浸下来学习,一直都是浮着. 至此大疫,给生命按下了暂停键. 踏踏实 ...
- Linux系统安装Redis(2020最新最详细)
2020最新Linux系统发行版ContOS7演示安装Redis 为防止操作权限不足,建议切换root用户,当然如果你对Linux命令熟悉,能够自主完成权限更新操作,可以不考虑此推荐. 更多命令学习推 ...
- update不能直接使用select的返回结果
update不能直接使用select的返回结果,这是会报错的,这是SQL的语法规定的,若想在update中与select结合使用,sql需要这样写: 1.其中field1,field2为表中的字段名 ...
- CUDA和cuDNN的安装
CUDA软件 Windows 打开NVIDIA CUDA网站,选择需要下载的版本,依次选择Windows平台,x86_64架构,10系统,exe(local)本地安装包,再选择Download即可下载 ...
- Ⅱ Finite Markov Decision Processes
Dictum: Is the true wisdom fortitude ambition. -- Napoleon 马尔可夫决策过程(Markov Decision Processes, MDPs ...