您的位置:首页 > 运维架构 > Shell

shell-grep命令、通配符、管道符

2020-06-03 04:46 429 查看

grep 命令

命令:grep  [选项] “关键词” 文件名      #行提取命令
-A 数字:列出符合条件的行,并将连续列出后续n行
-B 数字:列出符合条件的行,并将连续列出前面n行
-c  :统计包含字符串的行一共几行!
-i  :忽略大小写
-n  :输出行号
-v  :反向查找(取反)
--color=auto:搜索出的关键词高亮显示(默认别名)
[root@localhost ~]# grep "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
查看以下两行
[root@localhost ~]# grep -A 2 "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
--
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
标出行数
[root@localhost ~]# grep -n -A  2 "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
2-bin:x:1:1:bin:/bin:/sbin/nologin
3-daemon:x:2:2:daemon:/sbin:/sbin/nologin
--
10:operator:x:11:0:operator:/root:/sbin/nologin
11-games:x:12:100:games:/usr/games:/sbin/nologin
12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

find 和 grep 的区别
find 是在系统中查找符合条件的文件名,默认是完全匹配,如果需要模糊查找使用通配符。 grep 是在文件中查找符合条件的字符串,是包含匹配,如果需要精确查询,需要使用正则表达式,此处正则暂不介绍,高级篇中讲解。
:grep查询关键词时,只要包含关键词的行都被显示

通配符功能介绍

[root@localhost test]# touch abc 2abc abf sdfg
[root@localhost test]# ls
1.txt  2abc  2.txt  3.txt  4.txt  abc  abf  sdfg
[root@localhost test]# ls ?abc
2abc
[root@localhost test]# ls *abc
2abc  abc
[root@localhost test]# ls [0-9]*
1.txt  2abc  2.txt  3.txt  4.txt
[root@localhost test]# ls [^0-9]*
abc  abf  sdfg

管道符

格式:命令1 | 命令2
#将命令1的标准输出作为命令2的标准输入

[root@localhost test]# cat 1.txt
123456
123456
123456
[root@localhost test]# cat 1.txt | head -n 1
123456

格式:命令 |xargs 命令2
#将命令1的标准输出当做命令2的执行参数(执行对象),默认逐个处理

[root@localhost ~]# find /etc -name *.txt | cat
/etc/pki/nssdb/pkcs11.txt
[root@localhost ~]# find /etc -name *.txt |xargs cat
library=libnsssysinit.so
name=NSS Internal PKCS #11 Module
parameters=configdir='sql:/etc/pki/nssdb'  certPrefix='' keyPrefix='' secmod='secmod.db' flags= updatedir='' updateCertPrefix='' updateKeyPrefix='' updateid='' updateTokenDescription=''
NSS=Flags=internal,moduleDBOnly,critical trustOrder=75 cipherOrder=100 slotParams=(1={slotFlags=[RSA,DSA,DH,RC2,RC4,DES,RANDOM,SHA1,MD5,MD2,SSL,TLS,AES,Camellia,SEED,SHA256,SHA512] askpw=any timeout=30})

[root@localhost ~]#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐