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 ~]#
相关文章推荐
- Shell基础:介绍、历史命令、命令不全和别名、通配符、输入输入重定向、管道发和作业控制
- shell基础-输入输出以及重定向/多命令顺序执行与管道符/通配符与特殊符号
- Shell脚本基础之Bash的基本功能——历史命令(history)、命令和文件补全(Tab键)、命令别名(alias)、常用快捷键,输入输出重定向、多命令顺序执行、管道符、通配符与其他特殊符号
- Shell命令find和grep详解及正则表达式和通配符的区别
- 【Shell常用命令二】管道符 通配符
- Linux学习(二十一)Shell基础(一)认识shell、命令历史、命令补全、别名、通配符、管道符与前后台控制
- 基础知识 day-14 (8.1 shell介绍, 8.2 命令历史, 8.3 命令补全和别名, 8.4 通配符, 8.5 输入输出重定向, 8.6 管道符和作业控制 )
- LINUX系统学习笔记Shell基础(一)认识shell、命令历史、命令补全、别名、通配符、管道符与前后台控制
- 几个重要的shell命令:diff patch tar find grep
- grep命令(常用:与管道搭配,筛选出带某个关键词的输出)
- 【shell编程基础3】shell编程的组合应用之二:管道及其命令
- linux-命令替换-通配符-重定向-管道
- linux grep命令参数及用法详解---管道命令grep
- linux grep命令参数及用法详解---linux管道命令grep
- Shell学习:grep, sed, awk命令的练习题
- 【shell】grep命令及常见用法
- linux shell 管道命令(pipe)使用及与shell重定向区别
- shell ----- 文本处理 grep、cut、sort、uniq命令
- 在Perl中使用shell命令和管道
- Linux之常用Shell命令:grep,find