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

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 ~]# 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值