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' && ...
随机推荐
- mqtt网关
MQTT网关 MQTT网关是可以是将普通的串口数据.Modbus RTU数据等转化为MQTT协议的从而方便与平台的对接,通过连接服务器.订阅和发布主题来实现传统设备和MQTT云端的联系.例如,笔记本和 ...
- day53 Pyhton 前端04
内容回顾: 盒子: 内边距:padding,解决内部矛盾,内边距的增加整个盒子也会增加 外边距:margin,解决外部矛盾,当来盒子都有外边距的时候,取两者最大值 边框:border border-c ...
- day49 Pyhton 数据库Mysql 06
多表查询 连表查询 要进行连接,那一定涉及两个表,两个表中要有关联条件才能进行连接 内连接 只有表一和表二中的连接条件都满足的时候才能显示出来 inner join on /where 条件 sele ...
- 【UNR #2】UOJ拯救计划
UOJ小清新题表 题目内容 UOJ链接 题面太长了(其实是我懒得改LaTeX了) 一句话题意: 给出 \(n\) 个点和 \(m\) 条边,对其进行染色,共 \(k\) 种颜色,要求同一条边两点颜色不 ...
- LUMEN框架多数据库连接配置方法
LUMEN作为一款API导向很浓的框架,配置极简化,默认只支持一路DB配置 然而随着业务复杂度的提高,引入多个数据库连接似乎无法避免,下面介绍一下LUMEN连接多个数据库的配置方法: 修改.env文件 ...
- selenium 浏览器最大化
from time import sleep from selenium import webdriver from selenium.webdriver.chrome.options import ...
- 教你如何帮助前端同学快速生成API接口代码
最近我们团队开源的后端微服务框架go-zero增长势头比较迅猛,这篇文章我讲讲go-zero对前端团队的作用,并通过一个示例来给大家演示我们是怎么做的,希望能给后端的同学也可以帮助前端同学提高开发效率 ...
- Java中try()...catch()用法
在stackoverflow偶尔看到的一个关于try()...catch()的用法,通常我们使用try...catch()捕获异常的,如果遇到类似IO流的处理,要在finally部分关闭IO流,当然这 ...
- PHP实现Bitmap的探索 - GMP扩展使用
原文地址:https://blog.fanscore.cn/p/22/ 一.背景 公司当前有一个用户群的系统,核心功能是根据不同的条件组去不同的业务线中get符合条件的uid列表,然后存到redis中 ...
- MySQL全面瓦解2:常用命令和系统管理
常用命令 打开CMD命令窗口(记住使用管理员身份运行),我们就可以在命令窗口中做一些MySQL的命令操作了: 服务启动和关闭 这个我们上一个章节使用过了:net start mysql,这是服务命令, ...