Linux正规表达法

 

[日期:2009-06-01]来源:Linux社区  作者:Linux编辑

However, this dress is about $ 3183 dollars.

GNU is free air not free beer.

Her hair is very beauty.

I can’t finish the test.

Oh! The soup taste good.

motorcycle is cheap than car.

This window is clear.

the symbol '*' is represented as start.

Oh! My god!

The gd software is a library for drafting programs.

You are the best is mean you are the no. 1.

The world is the same with "glad".

I like dog.

google is the best tools for search keyword.

goooooogle yes!

go! go! Let's go.

# I am VBird

1)在test.txt文件中搜索the字符串,并显示行号

参考答案:

grep -n 'the' test.txt

2)在test.txt文件中反向搜索the字符串,并显示行号

参考答案:

grep -vn 'the' test.txt

3)在test.txt文件中搜索the字符串,显示行号并忽略大小写

参考答案:

grep -in 'the' test.txt

4)在test.txt文件中搜索oo字符串,但排除goo

参考答案:

grep -n '[^g]oo' test.txt

5)在test.txt文件中搜索oo字符串,但排除oo之前的小写字母

参考答案:

grep -n '[^a-z]oo' test.txt

6)在test.txt文件中搜索有数字的一行

参考答案:

grep -n [0-9] test.txt

7)在test.txt文件中只列出行首的the,并忽略大小写

参考答案:

grep -ni ^the test.txt

8)在test.txt文件中搜索开头不是英文字母的语句

参考答案:

grep -n ^[^a-zA-Z] test.txt

9)在test.txt文件中找到结尾为.的一行

参考答案:

grep -n '/.$' test.txt

10)在test.txt文件中查找空白行

参考答案:

grep -n '^$' test.txt

11)在test.txt文件中查找两个以上的o

参考答案:

grep -n 'ooo*' test.txt

12)在test.txt文件中查找以g开头,并且以g结尾的字符串

参考答案:

grep -n 'g.*g' test.txt

13)显示/etc/passwd文件,并且打印行号,同时去掉2至5行

参考答案:

nl /etc/passwd|sed '2,5d'

14)显示/etc/passwd文件,并且打印行号,在第二行后面加上drink tea

参考答案:

nl /etc/passwd | sed '2a drink tea'

15)显示/etc/passwd文件,并且打印行号,将2-5行替换为No 2-5 number

参考答案:

nl /etc/passwd | sed '2,5c No 2-5 number'

16)显示/etc/passwd文件的5-7行,打印地号

参考答案:

nl /etc/passwd | sed -n '5,7p'

17)显示/etc/passwd文件内容,将所有的sbin换成love

参考答案:

nl /etc/passwd|sed 's/sbin/love/g'

18)显示/etc/passwd文件内容,将只显示5-10行,并将5-10行的sbin换成love

参考答案:

nl /etc/passwd|sed '5,10 s/sbin/love/'|sed  -n '5,10p'

19)显示/etc/目录下所有文件的属性和文件名

如:attr=-rwxrwxrwx filename=passwd

参考答案:

ls -l /etc/|awk '{print "attr=" $1 "/t/tfilename=" $9}'|sed '1'd

20)显示所有登录用户,并显示登录用户的所在行数和列数

参考答案:

last | awk '{print $1 "/t lines: " NR "/t columes: " NF}'

21)只显示/etc/passwd文件的用户名和UID,且UID小于10

参考答案:

cat /etc/passwd|awk 'BEGIN {FS=":"} $3 < 10 {print $1 "/t " $3}'

22)根据使用频率列举 Shell 历史记录中的命令

参考答案:

history|awk '{print $2}'|awk 'BEGIN{FS="|"}{print $1}'|sort|uniq -c|sort -n

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值