特殊符号
符号 说明 [:alnum:] 英文大小写及数字 [;alpha:] 英文大小写 [:blank:] 空格键与tab键 [:cntrl:] 控制按键,如:tab,del等 [:digit:] 代表数字 [:graph:] 出了空格符(空格键&tab)外所有的按键 [:lower:] 小写字符 [:print:] 任何可以被打印的字符 [:punct:] 标点符号 [:upper:] 大写字符 [:space:] 空白字符 [:xdigit:] 十六进制
grep指令
grep [-A] [-B] ‘要搜索的字符串’ filename
参数说明:
-A:(after)后面带数字,列出指定列后面N列;
-B:(befer)后面带数字,列出指定列前面N列
可以同时使用
1.[root@localhost lcr]# vim test.txt
2.[root@localhost lcr]# cat test.txt
3.test-line-1
4.test-line-2
5.test-line-3
6.test-line-4
7.test-line-5
8.test-line-6
9.test-line-7
10.test-line-8
11.test-line-9
12.test-line-10
13.test-line-11
14.test-line-12
15.test-line-13
16.test-line-14
17.test-line-15
18.test-line-16
19.test-line-17
20.test-line-18
21.test-line-19
22.test-line-20
23.[root@localhost lcr]# grep -A 5 'test-line-5' test.txt
24.test-line-5
25.test-line-6
26.test-line-7
27.test-line-8
28.test-line-9
29.test-line-10
30.[root@localhost lcr]# grep -A 5 'line-6' test.txt #关键词不需要完全匹配某行
31.test-line-6
32.test-line-7
33.test-line-8
34.test-line-9
35.test-line-10
36.test-line-11
37.[root@localhost lcr]# grep -A5 -B4 'line-6' test.txt #使用两个参数
38.test-line-2
39.test-line-3
40.test-line-4
41.test-line-5
42.test-line-6
43.test-line-7
44.test-line-8
45.test-line-9
46.test-line-10
47.test-line-11
1.[root@localhost lcr]# dmesg | grep 'ens'
2.[ 0.813423] ACPI: Added _OSI(3.0 _SCP Extensions)
3.[ 20.640242] sd 2:0:0:0: [sda] Mode Sense: 61 00 00 00
4.[ 26.421599] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
5.[ 26.425384] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
6.[root@localhost lcr]# dmesg | grep -n 'ens' ##显示行号
7.633:[ 0.813423] ACPI: Added _OSI(3.0 _SCP Extensions)
8.1753:[ 20.640242] sd 2:0:0:0: [sda] Mode Sense: 61 00 00 00
9.1819:[ 26.421599] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
10.1820:[ 26.425384] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
11.[root@localhost lcr]# dmesg | grep -A3 -B3 'ens33' #指定显示前后
12.[ 26.295737] ip6_tables: (C) 2000-2006 Netfilter Core Team
13.[ 26.347233] Ebtables v2.0 registered
14.[ 26.402869] nf_conntrack version 0.5.0 (16384 buckets, 65536 max)
15.[ 26.421599] IPv6: ADDRCONF(NETDEV_UP): ens33: link is not ready
16.[ 26.425384] e1000: ens33 NIC Link is Up 1000 Mbps Full Duplex, Flow Control: None
17.[ 26.500396] bridge: automatic filtering via arp/ip/ip6tables has been deprecated. Update your scripts to load br_netfilter if you need this.
18.[ 26.520362] Netfilter messages via NETLINK v0.30.
19.[ 26.524557] ip_set: protocol 6
设定别名
grep='grep --color=auto'
1.[root@localhost lcr]# alias
2.alias cp='cp -i'
3.alias egrep='egrep --color=auto'
4.alias fgrep='fgrep --color=auto'
5.alias grep='grep --color=auto'
6.alias l.='ls -d .* --color=auto'
7.alias ll='ls -l --color=auto'
8.alias ls='ls --color=auto'
9.alias mv='mv -i'
10.alias rm='rm -i'
11.alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
1.[root@localhost lcr]# export | grep 'LANG'
2.declare -x GDM_LANG="en_US.utf8"
3.declare -x LANG="en_US.utf8"
一、正则表达式练习
1.[root@localhost lcr]# cat regular_express.txt
2."Open Source" is a good mechanism to develop programs.
3.apple is my favorite food.
4.Football game is not use feet only.
5.this dress doesn't fit me.
6.However, this dress is about $ 3183 dollars.
7.GNU is free air not free beer.
8.Her hair is very beauty.
9.I can't finish the test.
10.Oh! The soup taste good.
11.motorcycle is cheap than car.
12.This window is clear.
13.the symbol '*' is represented as start.
14.Oh! My god!
15.The gd software is a library for drafting programs.
16.You are the best is mean you are the no. 1.
17.The world <Happy> is the same with "glad".
18.I like dog.
19.google is the best tools for search keyword.
20.goooooogle yes!
21.go! go! Let's go.
22.# I am VBird
23.
1、查找指定字符
1.[root@localhost lcr]# grep -n 'the' regular_express.txt
2.8:I can't finish the test.
3.12:the symbol '*' is represented as start.
4.15:You are the best is mean you are the no. 1.
5.16:The world <Happy> is the same with "glad".
6.18:google is the best tools for search keyword.
1.[root@localhost lcr]# grep -vn 'the' regular_express.txt
2.1:"Open Source" is a good mechanism to develop programs.
3.2:apple is my favorite food.
4.3:Football game is not use feet only.
5.4:this dress doesn't fit me.
6.5:However, this dress is about $ 3183 dollars.
7.6:GNU is free air not free beer.
8.7:Her hair is very beauty.
9.9:Oh! The soup taste good.
10.10:motorcycle is cheap than car.
11.11:This window is clear.
12.13:Oh! My god!
13.14:The gd software is a library for drafting programs.
14.17:I like dog.
15.19:goooooogle yes!
16.20:go! go! Let's go.
17.21:# I am VBird
18.22:
19.
1.[root@localhost lcr]# grep -in 'the' regular_express.txt
2.8:I can't finish the test.
3.9:Oh! The soup taste good.
4.12:the symbol '*' is represented as start.
5.14:The gd software is a library for drafting programs.
6.15:You are the best is mean you are the no. 1.
7.16:The world <Happy> is the same with "glad".
8.18:google is the best tools for search keyword.
2、查找字符集
the
,taste
,相同的部分有t?st
1.[root@localhost lcr]# grep -n 't[ae]st' regular_express.txt
2.8:I can't finish the test.
3.9:Oh! The soup taste good.
[]里面不管有多少个字符,都只会按照一个字符单独组合
反向字符集
- 查找
oo
,但是前面不带g
1.[root@localhost lcr]# grep -n 'oo' regular_express.txt
2.1:"Open Source" is a good mechanism to develop programs.
3.2:apple is my favorite food.
4.3:Football game is not use feet only.
5.9:Oh! The soup taste good.
6.18:google is the best tools for search keyword.
7.19:goooooogle yes!
8.[root@localhost lcr]# grep -n '[^g]oo' regular_express.txt
9.2:apple is my favorite food.
10.3:Football game is not use feet only.
11.18:google is the best tools for search keyword.
12.19:goooooogle yes!
13.[root@localhost lcr]#
oo
,但是前面不带小写字母1.[root@localhost lcr]# grep -n '[^a-z]oo' regular_express.txt
2.3:Football game is not use feet only.
3.[root@localhost lcr]# grep -n '[:upper:]oo' regular_express.txt
4.grep: character class syntax is [[:space:]], not [:space:]
5.[root@localhost lcr]# grep -n '[[:upper:]]oo' regular_express.txt
6.3:Football game is not use feet only.
3、连续字符集
1.[root@localhost lcr]# grep -n [0-9] regular_express.txt
2.5:However, this dress is about $ 3183 dollars.
3.15:You are the best is mean you are the no. 1.
1.[root@localhost lcr]# grep -n '[^[:lower:]]' regular_express.txt
2.1:"Open Source" is a good mechanism to develop programs.
3.2:apple is my favorite food.
4.3:Football game is not use feet only.
5.4:this dress doesn't fit me.
6.
7.
8.
9.
10.
11.10:motorcycle is cheap than car.
12.11:This window is clear.
13.12:the symbol '*' is represented as start.
14.13:Oh! My god!
15.
16.15:You are the best is mean you are the no. 1.
17.16:The world <Happy> is the same with "glad".
18.17:I like dog.
19.18:google is the best tools for search keyword.
20.19:goooooogle yes!
21.20:go! go! Let's go.
22.21:# I am VBird
这个不符合设定的目的
设置
1.export LANG=C
1.[root@localhost lcr]# grep -n '[^[:lower:]]oo' regular_express.txt
2.3:Football game is not use feet only.
4、查找行首,行末
使用制表符
^
1.[root@localhost lcr]# grep -n '^the' regular_express.txt
2.12:the symbol '*' is represented as start.
1.[root@localhost lcr]# grep -n '^[a-z]' regular_express.txt
2.2:apple is my favorite food.
3.4:this dress doesn't fit me.
4.10:motorcycle is cheap than car.
5.12:the symbol '*' is represented as start.
6.18:google is the best tools for search keyword.
7.19:goooooogle yes!
8.20:go! go! Let's go.
有别于[^a-z]
1.[root@localhost lcr]# grep -n '[^a-z]' regular_express.txt
2.1:"Open Source" is a good mechanism to develop programs.
3.2:apple is my favorite food.
4.3:Football game is not use feet only.
5.4:this dress doesn't fit me.
6.
7.
8.
9.
10.
11.10:motorcycle is cheap than car.
12.11:This window is clear.
13.12:the symbol '*' is represented as start.
14.13:Oh! My god!
15.
16.15:You are the best is mean you are the no. 1.
17.16:The world <Happy> is the same with "glad".
18.17:I like dog.
19.18:google is the best tools for search keyword.
20.19:goooooogle yes!
21.20:go! go! Let's go.
22.21:# I am VBird
开头不要字母
1.[root@localhost lcr]# grep -n '^[^a-zA-Z]' regular_express.txt
2.1:"Open Source" is a good mechanism to develop programs.
3.21:# I am VBird
^
在[]
内外的含义不同,在内表示反向选择,在外表示行首
使用
$
1.[root@localhost lcr]# grep -n '\.$' regular_express.txt
2.1:"Open Source" is a good mechanism to develop programs.
3.2:apple is my favorite food.
4.3:Football game is not use feet only.
5.4:this dress doesn't fit me.
6.10:motorcycle is cheap than car.
7.11:This window is clear.
8.12:the symbol '*' is represented as start.
9.15:You are the best is mean you are the no. 1.
10.16:The world <Happy> is the same with "glad".
11.17:I like dog.
12.18:google is the best tools for search keyword.
13.20:go! go! Let's go.
因为
.
有特别的含义,所以需要反义符
部分行末尾是.
但是没有显示出来
1.[root@localhost lcr]# cat -An regular_express.txt | head -n 10 | tail -n 62. 5 However, this dress is about $ 3183 dollars.^M$3. 6 GNU is free air not free beer.^M$4. 7 Her hair is very beauty.^M$5. 8 I can't finish the test.^M$6. 9 Oh! The soup taste good.^M$7. 10 motorcycle is cheap than car.$
1.[root@localhost lcr]# grep -n '^$' regular_express.txt
2.22:
.
(小数点)表示任意字符
1.[root@localhost lcr]# grep -n 'g..d' regular_express.txt
2.1:"Open Source" is a good mechanism to develop programs.
3.9:Oh! The soup taste good.
4.16:The world <Happy> is the same with "glad".
*
(星号)表示重复字符
- 一个:
o*
:可有可无(没有意义)
1.[root@localhost lcr]# grep -n 'o*' regular_express.txt
2.1:"Open Source" is a good mechanism to develop programs.
3.2:apple is my favorite food.
4.3:Football game is not use feet only.
5.4:this dress doesn't fit me.
6.5:However, this dress is about $ 3183 dollars.
7.6:GNU is free air not free beer.
8.7:Her hair is very beauty.
9.8:I can't finish the test.
10.9:Oh! The soup taste good.
11.10:motorcycle is cheap than car.
12.11:This window is clear.
13.12:the symbol '*' is represented as start.
14.13:Oh! My god!
15.14:The gd software is a library for drafting programs.
16.15:You are the best is mean you are the no. 1.
17.16:The world <Happy> is the same with "glad".
18.17:I like dog.
19.18:google is the best tools for search keyword.
20.19:goooooogle yes!
21.20:go! go! Let's go.
22.21:# I am VBird
23.22:
- 两个:
oo*
:表示至少有一个o
1.[root@localhost lcr]# grep -n 'oo*' regular_express.txt
2.1:"Open Source" is a good mechanism to develop programs.
3.2:apple is my favorite food.
4.3:Football game is not use feet only.
5.4:this dress doesn't fit me.
6.5:However, this dress is about $ 3183 dollars.
7.6:GNU is free air not free beer.
8.9:Oh! The soup taste good.
9.10:motorcycle is cheap than car.
10.11:This window is clear.
11.12:the symbol '*' is represented as start.
12.13:Oh! My god!
13.14:The gd software is a library for drafting programs.
14.15:You are the best is mean you are the no. 1.
15.16:The world <Happy> is the same with "glad".
16.17:I like dog.
17.18:google is the best tools for search keyword.
18.19:goooooogle yes!
19.20:go! go! Let's go.
- 三个:
ooo*
:表示至少有2个重复的o
1.[root@localhost lcr]# grep -n 'ooo*' regular_express.txt
2.1:"Open Source" is a good mechanism to develop programs.
3.2:apple is my favorite food.
4.3:Football game is not use feet only.
5.9:Oh! The soup taste good.
6.18:google is the best tools for search keyword.
7.19:goooooogle yes!
- 四个:
oooo*
:表示至少有三个重复的o
1.[root@localhost lcr]# grep -n 'oooo*' regular_express.txt
2.19:goooooogle yes!
比如查找g
开头结尾的字符
1.[root@localhost lcr]# grep -n 'g.g' regular_express.txt
2.[root@localhost lcr]# grep -n 'g.*g' regular_express.txt
3.1:"Open Source" is a good mechanism to develop programs.
4.14:The gd software is a library for drafting programs.
5.18:google is the best tools for search keyword.
6.19:goooooogle yes!
7.20:go! go! Let's go.
“
.*
” :代表林哥或多个任意字符
o
1.[root@localhost lcr]# grep -n 'o\{2\}' regular_express.txt
2.1:"Open Source" is a good mechanism to develop programs.
3.2:apple is my favorite food.
4.3:Football game is not use feet only.
5.9:Oh! The soup taste good.
6.18:google is the best tools for search keyword.
7.19:goooooogle yes!
1.[root@localhost lcr]# grep -n 'o\{4\}' regular_express.txt
2.19:goooooogle yes!
1.[root@localhost lcr]# grep -n 'go\{2,5\}g' regular_express.txt
2.18:google is the best tools for search keyword.
1.[root@localhost lcr]# grep -n 'go\{2,\}g' regular_express.txt
2.18:google is the best tools for search keyword.
3.19:goooooogle yes!
1.[root@localhost lcr]# grep -n 'go\{,5\}g' regular_express.txt
2.18:google is the best tools for search keyword.
1.[root@localhost lcr]# grep -n 'go\{,5\}' regular_express.txt
2.1:"Open Source" is a good mechanism to develop programs.
3.3:Football game is not use feet only.
4.9:Oh! The soup taste good.
5.13:Oh! My god!
6.14:The gd software is a library for drafting programs.
7.16:The world <Happy> is the same with "glad".
8.17:I like dog.
9.18:google is the best tools for search keyword.
10.19:goooooogle yes!
11.20:go! go! Let's go.
二、sed 工具
sed也是一个管道命令,可以分析standard INPUT,可以替换,删除,新增,选取特定行等功能
1、指令解析
简易说明
1.[root@localhost lcr]# sed
2.Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
3.
4. -n, --quiet, --silent
5. suppress automatic printing of pattern space
6. -e script, --expression=script
7. add the script to the commands to be executed
8. -f script-file, --file=script-file
9. add the contents of script-file to the commands to be executed
10. --follow-symlinks
11. follow symlinks when processing in place
12. -i[SUFFIX], --in-place[=SUFFIX]
13. edit files in place (makes backup if SUFFIX supplied)
14. -c, --copy
15. use copy instead of rename when shuffling files in -i mode
16. -b, --binary
17. does nothing; for compatibility with WIN32/CYGWIN/MSDOS/EMX (
18. open files in binary mode (CR+LFs are not treated specially))
19. -l N, --line-length=N
20. specify the desired line-wrap length for the `l' command
21. --posix
22. disable all GNU extensions.
23. -r, --regexp-extended
24. use extended regular expressions in the script.
25. -s, --separate
26. consider files as separate rather than as a single continuous
27. long stream.
28. -u, --unbuffered
29. load minimal amounts of data from the input files and flush
30. the output buffers more often
31. -z, --null-data
32. separate lines by NUL characters
33. --help
34. display this help and exit
35. --version
36. output version information and exit
37.
38.If no -e, --expression, -f, or --file option is given, then the first
39.non-option argument is taken as the sed script to interpret. All
40.remaining arguments are names of input files; if no input files are
41.specified, then the standard input is read.
42.
43.GNU sed home page: <http://www.gnu.org/software/sed/>.
44.General help using GNU software: <http://www.gnu.org/gethelp/>.
参数说明
参数
参数 说明 -n 安静模式,只有经过sed处理的才会显示 -e 在命令行模式编辑sed的动作 -f 执行文件内的sed动作 -r 支持扩展性正则表达式的语法 -i 自己修改读取的文件内容,而不输出到屏幕
动作
[n1[,n2]]function
n1,n2:选择进行处理的行数
动作参数
参数 说明 a add,增加可以接字符串,加在当前行的下一行 c change,替换,可接字符串,可以替换n1,n2之间的行 d delete,删除,一般不接内容 i insert,插入,可接字符串,插入当前行的上一行 p print,打印,将选定的数据输到屏幕上通常与sed -n同时使用 s switch,替换,通常与正则表达式配合使用
1.[root@localhost etc]# nl passwd | sed '2,5d'
2. 1 root:x:0:0:root:/root:/bin/bash
3. 6 sync:x:5:0:sync:/sbin:/bin/sync
4. 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
5. 8 halt:x:7:0:halt:/sbin:/sbin/halt
6. 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
7. 10 operator:x:11:0:operator:/root:/sbin/nologin
8. 11 games:x:12:100:games:/usr/games:/sbin/nologin
9. 12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
10. 13 nobody:x:99:99:Nobody:/:/sbin/nologin
11. 14 pegasus:x:66:65:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/sbin/nologin
12. 15 ods:x:999:998:softhsm private keys owner:/var/lib/softhsm:/sbin/nologin
13. 16 systemd-bus-proxy:x:998:996:systemd Bus Proxy:/:/sbin/nologin
14. 17 systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
15. ......
2-5行不显示
1.[root@localhost etc]# nl passwd | sed '2a drink tea'
2. 1 root:x:0:0:root:/root:/bin/bash
3. 2 bin:x:1:1:bin:/bin:/sbin/nologin
4.drink tea
5. 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
6. 4 adm:x:3:4:adm:/var/adm:/sbin/nologin
7. 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
8. 6 sync:x:5:0:sync:/sbin:/bin/sync
9. 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
10. 8 halt:x:7:0:halt:/sbin:/sbin/halt
11. 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
12. 10 operator:x:11:0:operator:/root:/sbin/nologin
13. 11 games:x:12:100:games:/usr/games:/sbin/nologin
14. 12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
1.[root@localhost etc]# nl passwd | sed '2a drink tea or ...........\>drink beer?'
2. 1 root:x:0:0:root:/root:/bin/bash
3. 2 bin:x:1:1:bin:/bin:/sbin/nologin
4.drink tea or ...........>drink beer?
5. 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
6. 4 adm:x:3:4:adm:/var/adm:/sbin/nologin
7. 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
8. 6 sync:x:5:0:sync:/sbin:/bin/sync
9. 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
10. 8 halt:x:7:0:halt:/sbin:/sbin/halt
11. 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
12.........
13.[root@localhost etc]# nl passwd | sed '2a drink tea or ..\ #此处要回车另起一行输入
14.> drink beer?'
15. 1 root:x:0:0:root:/root:/bin/bash
16. 2 bin:x:1:1:bin:/bin:/sbin/nologin
17.drink tea or ..
18.drink beer?
19. 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
20. 4 adm:x:3:4:adm:/var/adm:/sbin/nologin
21. 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
22. 6 sync:x:5:0:sync:/sbin:/bin/sync
23. 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
24. 8 halt:x:7:0:halt:/sbin:/sbin/halt
25. 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
26........
1.[root@localhost etc]# nl passwd | sed '2,5c NO 2-5 number'
2. 1 root:x:0:0:root:/root:/bin/bash
3.NO 2-5 number
4. 6 sync:x:5:0:sync:/sbin:/bin/sync
5. 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
6. 8 halt:x:7:0:halt:/sbin:/sbin/halt
7. 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
8. 10 operator:x:11:0:operator:/root:/sbin/nologin
9. 11 games:x:12:100:games:/usr/games:/sbin/nologin
10. 12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
11.......
1.[root@localhost etc]# nl passwd | sed -n '3,8p'
2. 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
3. 4 adm:x:3:4:adm:/var/adm:/sbin/nologin
4. 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
5. 6 sync:x:5:0:sync:/sbin:/bin/sync
6. 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
7. 8 halt:x:7:0:halt:/sbin:/sbin/halt
sed -n,这个先阻止了输出到屏幕,‘3,8p’,打印3-8行
1.[root@localhost etc]# nl passwd | sed '5,8p'
2. 1 root:x:0:0:root:/root:/bin/bash
3. 2 bin:x:1:1:bin:/bin:/sbin/nologin
4. 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin
5. 4 adm:x:3:4:adm:/var/adm:/sbin/nologin
6. 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
7. 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
8. 6 sync:x:5:0:sync:/sbin:/bin/sync
9. 6 sync:x:5:0:sync:/sbin:/bin/sync
10. 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
11. 7 shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
12. 8 halt:x:7:0:halt:/sbin:/sbin/halt
13. 8 halt:x:7:0:halt:/sbin:/sbin/halt
14. 9 mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
15. 10 operator:x:11:0:operator:/root:/sbin/nologin
16. 11 games:x:12:100:games:/usr/games:/sbin/nologin
17. 12 ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
如果不加上
-n
,则会全部显示,在此基础上,5-8行显示多一次,则会造成重复,而不是选择输出显示
以网卡信息的处理为例
1.[root@localhost etc]# ifconfig
2.ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
3. inet 192.168.121.7 netmask 255.255.255.0 broadcast 192.168.121.255
4. inet6 fe80::6cc1:deec:46c3:c576 prefixlen 64 scopeid 0x20<link>
5. ether 00:0c:29:50:7c:3b txqueuelen 1000 (Ethernet)
6. RX packets 1428 bytes 136309 (133.1 KiB)
7. RX errors 0 dropped 0 overruns 0 frame 0
8. TX packets 362 bytes 31363 (30.6 KiB)
9. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
10.
11.lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
12. inet 127.0.0.1 netmask 255.0.0.0
13. inet6 ::1 prefixlen 128 scopeid 0x10<host>
14. loop txqueuelen 1 (Local Loopback)
15. RX packets 72 bytes 6252 (6.1 KiB)
16. RX errors 0 dropped 0 overruns 0 frame 0
17. TX packets 72 bytes 6252 (6.1 KiB)
18. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
19.
20.virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
21. inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
22. ether 52:54:00:02:b2:34 txqueuelen 1000 (Ethernet)
23. RX packets 0 bytes 0 (0.0 B)
24. RX errors 0 dropped 0 overruns 0 frame 0
25. TX packets 0 bytes 0 (0.0 B)
26. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
1.[root@localhost etc]# ifconfig | sed -n '2p'
2. inet 192.168.121.7 netmask 255.255.255.0 broadcast 192.168.121.255
这样数据就只剩下第二行
也可以使用正则表达式
1.[root@localhost etc]# ifconfig ens33 | grep 'inet 1'
2. inet 192.168.121.7 netmask 255.255.255.0 broadcast 192.168.121.255
三、扩展正值表达式
1.[root@localhost lcr]# grep -v '^$' regular_express.txt | grep -v '^#'
2."Open Source" is a good mechanism to develop programs.
3.apple is my favorite food.
4.Football game is not use feet only.
5.this dress doesn't fit me.
6.However, this dress is about $ 3183 dollars.
7.GNU is free air not free beer.
8.Her hair is very beauty.
9.I can't finish the test.
10.Oh! The soup taste good.
11.motorcycle is cheap than car.
12.This window is clear.
13.the symbol '*' is represented as start.
14.Oh! My god!
15.The gd software is a library for drafting programs.
16.You are the best is mean you are the no. 1.
17.The world <Happy> is the same with "glad".
18.I like dog.
19.google is the best tools for search keyword.
20.goooooogle yes!
21.go! go! Let's go.
- 上面的没有行号,尝试添加行号
1.[root@localhost lcr]# grep -nv '^$' regular_express.txt | grep -v '^#'
2.1:"Open Source" is a good mechanism to develop programs.
3.2:apple is my favorite food.
4.3:Football game is not use feet only.
5.4:this dress doesn't fit me.
6.5:However, this dress is about $ 3183 dollars.
7.6:GNU is free air not free beer.
8.7:Her hair is very beauty.
9.8:I can't finish the test.
10.9:Oh! The soup taste good.
11.10:motorcycle is cheap than car.
12.11:This window is clear.
13.12:the symbol '*' is represented as start.
14.13:Oh! My god!
15.14:The gd software is a library for drafting programs.
16.15:You are the best is mean you are the no. 1.
17.16:The world <Happy> is the same with "glad".
18.17:I like dog.
19.18:google is the best tools for search keyword.
20.19:goooooogle yes!
21.20:go! go! Let's go.
22.21:# I am VBird
上面显示行号了,但是第21行,猜测是因为第一遍过滤的时候添加了行号,改变了开头
修改为在第二遍过滤的时候添加行号
1.[root@localhost lcr]# grep -v '^$' regular_express.txt | grep -nv '^#'
2.1:"Open Source" is a good mechanism to develop programs.
3.2:apple is my favorite food.
4.3:Football game is not use feet only.
5.4:this dress doesn't fit me.
6.5:However, this dress is about $ 3183 dollars.
7.6:GNU is free air not free beer.
8.7:Her hair is very beauty.
9.8:I can't finish the test.
10.9:Oh! The soup taste good.
11.10:motorcycle is cheap than car.
12.11:This window is clear.
13.12:the symbol '*' is represented as start.
14.13:Oh! My god!
15.14:The gd software is a library for drafting programs.
16.15:You are the best is mean you are the no. 1.
17.16:The world <Happy> is the same with "glad".
18.17:I like dog.
19.18:google is the best tools for search keyword.
20.19:goooooogle yes!
21.20:go! go! Let's go.
这次正常显示
1.[root@localhost lcr]# egrep -v '^$|^#' regular_express.txt
2."Open Source" is a good mechanism to develop programs.
3.apple is my favorite food.
4.Football game is not use feet only.
5.this dress doesn't fit me.
6.However, this dress is about $ 3183 dollars.
7.GNU is free air not free beer.
8.Her hair is very beauty.
9.I can't finish the test.
10.Oh! The soup taste good.
11.motorcycle is cheap than car.
12.This window is clear.
13.the symbol '*' is represented as start.
14.Oh! My god!
15.The gd software is a library for drafting programs.
16.You are the best is mean you are the no. 1.
17.The world <Happy> is the same with "glad".
18.I like dog.
19.google is the best tools for search keyword.
20.goooooogle yes!
21.go! go! Let's go.
22.[root@localhost lcr]# egrep -nv '^$|^#' regular_express.txt
23.1:"Open Source" is a good mechanism to develop programs.
24.2:apple is my favorite food.
25.3:Football game is not use feet only.
26.4:this dress doesn't fit me.
27.5:However, this dress is about $ 3183 dollars.
28.6:GNU is free air not free beer.
29.7:Her hair is very beauty.
30.8:I can't finish the test.
31.9:Oh! The soup taste good.
32.10:motorcycle is cheap than car.
33.11:This window is clear.
34.12:the symbol '*' is represented as start.
35.13:Oh! My god!
36.14:The gd software is a library for drafting programs.
37.15:You are the best is mean you are the no. 1.
38.16:The world <Happy> is the same with "glad".
39.17:I like dog.
40.18:google is the best tools for search keyword.
41.19:goooooogle yes!
42.20:go! go! Let's go.
43.[root@localhost lcr]#
其中单引号内的
|
为“或”的意思
grep -E
grep -E 类似于egrep的别名
1.[root@localhost lcr]# grep -E -v '^$|^#' regular_express.txt
2."Open Source" is a good mechanism to develop programs.
3.apple is my favorite food.
4.Football game is not use feet only.
5.this dress doesn't fit me.
6.However, this dress is about $ 3183 dollars.
7.GNU is free air not free beer.
8.Her hair is very beauty.
9.I can't finish the test.
10.Oh! The soup taste good.
11.motorcycle is cheap than car.
12.This window is clear.
13.the symbol '*' is represented as start.
14.Oh! My god!
15.The gd software is a library for drafting programs.
16.You are the best is mean you are the no. 1.
17.The world <Happy> is the same with "glad".
18.I like dog.
19.google is the best tools for search keyword.
20.goooooogle yes!
21.go! go! Let's go.
1.[root@localhost lcr]# grep -Env '^$|^#' regular_express.txt
2.1:"Open Source" is a good mechanism to develop programs.
3.2:apple is my favorite food.
4.3:Football game is not use feet only.
5.4:this dress doesn't fit me.
6.5:However, this dress is about $ 3183 dollars.
7.6:GNU is free air not free beer.
8.7:Her hair is very beauty.
9.8:I can't finish the test.
10.9:Oh! The soup taste good.
11.10:motorcycle is cheap than car.
12.11:This window is clear.
13.12:the symbol '*' is represented as start.
14.13:Oh! My god!
15.14:The gd software is a library for drafting programs.
16.15:You are the best is mean you are the no. 1.
17.16:The world <Happy> is the same with "glad".
18.17:I like dog.
19.18:google is the best tools for search keyword.
20.19:goooooogle yes!
21.20:go! go! Let's go.
2、RE字符
+
重复一个或者多个前一个字符
eg:查找god,good,goood
1.1:"Open Source" is a good mechanism to develop programs.
2.9:Oh! The soup taste good.
3.13:Oh! My god!
3、RE字符
?
零个或者一个起哪一个字符
eg:查找gd,god
1.[root@localhost lcr]# egrep -n 'go?d' regular_express.txt
2.13:Oh! My god!
3.14:The gd software is a library for drafting programs.
4、RE字符
|
或条件
eg:查找gd或者good
1.[root@localhost lcr]# egrep -n 'gd|god' regular_express.txt
2.13:Oh! My god!
3.14:The gd software is a library for drafting programs.
5、RE字符
()
“组”
eg:查找glad或者good
1.[root@localhost lcr]# egrep -n 'g(la|oo)d' regular_express.txt
2.1:"Open Source" is a good mechanism to develop programs.
3.9:Oh! The soup taste good.
4.16:The world <Happy> is the same with "glad".
1.[root@localhost lcr]# egrep -n 'g(la|o)d' regular_express.txt
2.13:Oh! My god!
3.16:The world <Happy> is the same with "glad".
+
1.[root@localhost lcr]# egrep -n 'g(la|o+)d' regular_express.txt
2.1:"Open Source" is a good mechanism to develop programs.
3.9:Oh! The soup taste good.
4.13:Oh! My god!
5.16:The world <Happy> is the same with "glad".
*
1.[root@localhost lcr]# egrep -n 'g(la|o*)d' regular_express.txt
2.1:"Open Source" is a good mechanism to develop programs.
3.9:Oh! The soup taste good.
4.13:Oh! My god!
5.14:The gd software is a library for drafting programs.
6.16:The world <Happy> is the same with "glad".
6、RE字符
()+
多个重复的组
1.[root@localhost lcr]# echo 'AxyzxyzxyzxyzxyzC' | egrep 'A(xyz)+C'
2.AxyzxyzxyzxyzxyzC
bash特点:
- 命令记忆
执行过的命令会记忆下来,
- 别名功能(alias)
- 查看已有的别名
1.[root@localhost lcr]# alias
2.alias cp='cp -i'
3.alias egrep='egrep --color=auto'
4.alias fgrep='fgrep --color=auto'
5.alias grep='grep --color=auto'
6.alias l.='ls -d .* --color=auto'
7.alias ll='ls -l --color=auto'
8.alias lm='ls -al'
9.alias ls='ls --color=auto'
10.alias mv='mv -i'
11.alias rm='rm -i'
12.alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
1.[root@localhost lcr]# alias lm='ls -al'
2.[root@localhost lcr]# lm
3.total 312
4.drwx------. 19 lcr lcr 4096 Oct 7 19:58 .
5.drwxr-xr-x. 4 root root 4096 Sep 28 21:22 ..
6.-rw-------. 1 lcr lcr 749 Oct 6 21:03 .bash_history
7.-rw-r--r--. 1 lcr lcr 18 Aug 3 2016 .bash_logout
8.-rw-r--r--. 1 lcr lcr 193 Aug 3 2016 .bash_profile
9.-rw-r--r--. 1 lcr lcr 231 Aug 3 2016 .bashrc
10.drwx------. 12 lcr lcr 4096 Oct 5 23:48 .cache
11.drwxr-xr-x. 18 lcr lcr 4096 Oct 6 21:13 .config
12.drwxr-xr-x. 2 lcr lcr 4096 Sep 28 21:45 Desktop
13.drwxr-xr-x. 7 lcr lcr 4096 Oct 1 10:57 Documents
14.drwxr-xr-x. 2 lcr lcr 4096 Sep 28 21:45 Downloads
15....
1.[root@localhost lcr]# unalias lm