三剑客之grep

三剑客之grep         三剑客之sed         三剑客之awk

1 通配符

*:匹配所以字符。
{}:生成序列。
[][a-z]匹配一个小写字母;1个中括号相当于1个字符.
[^]:取反排除。
?:匹配任意一个字符。

1.找出当前目录下以.txt结尾的文件。
> ls *.txt

2.找出/etc/下面以.conf结尾的文件。
> find /etc -type f -name "*.conf"

3.找出/etc/下面包含co的文件。
> find /etc -type f -name "*co*"

4.从1开始,步长35,加到100为止。
> seq 1 35 100
1
36
71

5.从1开始,步长35,加到100为止。
> echo {1..100..35}
1 36 71

6.布拉布拉布拉
>echo {1..10}
1 2 3 4 5 6 7 8 9 10

7.布拉布拉布拉
>echo {01..10}
01 02 03 04 05 06 07 08 09 10

8.布拉布拉布拉
>echo liming{01..10}
liming01 liming02 liming03 liming04 liming05 liming06 liming07 liming08 liming09 liming10

9.布拉布拉布拉
>echo a.txt{,.bak}
a.txt a.txt.bak

10.布拉布拉布拉
>cp a.txt{,.bak}
>ls a.*
a.txt  a.txt.bak

11.查看文件内容时在行结尾添加 $ 符号
cat -A a.txt

2 单引号’',双引号"",不加引号,反引号``

单引号'':所见即所得,单引号里的内容会被原封不动输出(大部分命令)。
双引号"":双引号里面的特殊符号会被解析运行;反引号``,$()$
不加引号:与双引号类似,支持通配符。
反引号``:优先执行反引号里面的命令。

> echo 'michael $LANG `hostname` $(whoami) {1..4}'
michael $LANG `hostname` $(whoami) {1..4}

> echo "michael $LANG `hostname` $(whoami) {1..4}"
michael en_US.UTF-8 michael root {1..4}

> echo michael $LANG `hostname` $(whoami) {1..4}
michael en_US.UTF-8 michael root 1 2 3 4

> echo `michael $LANG hostname $(whoami) {1..4}`
报错-bash: michael: command not found

3 Regular Expression(RE)正则表达式

基础正则:^$^$.*.*[][^]   BRE Basic
扩展正则:|+{}()?                               ERE Extended

\   :去除特殊含义。
\n :回车换行。
\t :制表符。

1.匹配空行
> grep ^$ a.txt
2.匹配空行
> grep '^$' a.txt
3.匹配空行,并显示行号
> grep -n ^$ a.txt
4.匹配空行,并显示行号
> grep -n '^$' a.txt

5.排除匹配空行,并显示行号;-v 反向匹配
> grep -v -n '^$' a.txt
> grep -vn '^$' a.txt

6.转义符 显示 与 转义
> echo 'michael \n michelle'
michael \n michelle
> echo -e 'michael \n michelle'
michael 
 michelle

7.匹配出 所有开头到a的内容
grep '^.*a' test.txt 

8.匹配出 所有包含a或b或c的行的内容
grep '[abc]' test.txt

8.匹配出所有包含a或b或c的行的内容(不区分大小写)
grep -i '[abc]' test.txt

8.匹配出所有包含a-z,A-Z,0-9的行的内容(不区分大小写)
grep -i '[a-zA-Z0-9]' test.txt

8.匹配出所有包含a-z,A-Z,0-9的行的内容(不区分大小写)[按每个匹配的列出]
  show only the part of a line matching PATTERN
grep -i -o '[a-zA-Z0-9]' test.txt

9.grep -E 同时匹配多个关键字(或关系);
   匹配 file.txt 中包含 word1 或 word2 或 word3 的行。
   满足其中任意条件(word1、word2和word3之一)就会匹配。
grep -E "word1|word2|word3" test.txt

10.同时匹配多个关键字(与关系);
   必须同时满足三个条件(word1、word2和word3)才匹配。
grep word1 test.txt | grep word2 |grep word3

3 grep

> grep "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

> grep --color "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

> grep -n --color "root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin

> grep -n --color "^root" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash

> grep -n --color "bash$" /etc/passwd
1:root:x:0:0:root:/root:/bin/bash

> grep -v "#" /etc/e2fsck.conf | grep -v "^$"
[options]
broken_system_clock = 1

4 egrep

egrep = grep -E

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值