08-文件过滤排序命令grep,sort,which
Posted 人生玩家
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了08-文件过滤排序命令grep,sort,which相关的知识,希望对你有一定的参考价值。
一、
1、“|"管道符
[root@yyds <sub>]# cat ./abc.txt
Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA83755:11/12/56:20300
Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
Igor Chevsky:385-375-8395:3567 Populus Place, Caldwell, NJ 23875:6/18/68:23400
Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI23874:3/28/45:245700
Jennifer Cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900
Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100
Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92086:7/25/53:85100
Karen Evich:284-758-2867:23 edgecliff Place, lincoln, NB 92743:11/3/35:58200
Karen Evich:284-758-2867:23 EdgEcliff Place, LinColn, NB 92743:11/3/35:58200
Fred Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/12/23:780900
[root@yyds </sub>]# cat ./abc.txt | grep . -n ./abc.txt
1:Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA83755:11/12/56:20300
2:Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
3:Igor Chevsky:385-375-8395:3567 Populus Place, Caldwell, NJ 23875:6/18/68:23400
4:Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI23874:3/28/45:245700
6:Jennifer Cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900
7:Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100
8:Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92086:7/25/53:85100
9:Karen Evich:284-758-2867:23 edgecliff Place, lincoln, NB 92743:11/3/35:58200
10:Karen Evich:284-758-2867:23 EdgEcliff Place, LinColn, NB 92743:11/3/35:58200
11:Fred Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/
2、grep:过滤输出内容
(1)要过滤的标注部分
^ ;以...开头,如:^a,以a开头
_$:以..结尾, 如:b$,以结尾
*:匹配*前面内容至少0次或多少。
.:匹配任意字符
(2)选项
#选项:
-n:打印行号 number
-A:after 查看过滤内容的后N行
-B:before 查看过滤内容的前N行
-C:center 查看过滤内容的前、后N行
-v:取反
-E:可以过滤多个结果
-o:只打印出过滤内容
-i:不区分大小写
# ① -n number 打印行号
[root@yyds <sub>]# grep ^[a-Z] -n ./abc.txt
1:Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA83755:11/12/56:20300
2:Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
3:Igor Chevsky:385-375-8395:3567 Populus Place, Caldwell, NJ 23875:6/18/68:23400
4:Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI23874:3/28/45:245700
6:Jennifer Cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900
7:Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100
8:Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92086:7/25/53:85100
9:Karen Evich:284-758-2867:23 edgecliff Place, lincoln, NB 92743:11/3/35:58200
10:Karen Evich:284-758-2867:23 EdgEcliff Place, LinColn, NB 92743:11/3/35:58200
11:Fred Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/12/23:780900
12:fengdatianliang
13:smile of life
#^... 以..开头
[root@yyds </sub>]# grep ^K abc.txt
Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92086:7/25/53:85100
Karen Evich:284-758-2867:23 edgecliff Place, lincoln, NB 92743:11/3/35:58200
Karen Evich:284-758-2867:23 EdgEcliff Place, LinColn, NB 92743:11/3/35:58200
#..$ 以..结尾
[root@yyds ~]# grep 0$ abc.txt
Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA83755:11/12/56:20300
Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
Igor Chevsky:385-375-8395:3567 Populus Place, Caldwell, NJ 23875:6/18/68:23400
Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI23874:3/28/45:245700
Jennifer Cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900
Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100
Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92086:7/25/53:85100
Karen Evich:284-758-2867:23 edgecliff Place, lincoln, NB 92743:11/3/35:58200
Karen Evich:284-758-2867:23 EdgEcliff Place, LinColn, NB 92743:11/3/35:58200
$Fred Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/12/23:780900
# \\的用法,含$的内容高亮显示
对比:
[root@yyds ~]# cat abc.txt
Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA83755:11/12/56:20300
Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500
Igor Chevsky:385-375-8395:3567 Populus Place, Caldwell, NJ 23875:6/18/68:23400
Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI23874:3/28/45:245700
Jennifer Cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900
Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100
Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92086:7/25/53:85100
Karen Evich:284-758-2867:23 edgecliff Place, lincoln, NB 92743:11/3/35:58200
Karen Evich:284-758-2867:23 EdgEcliff Place, LinColn, NB 92743:11/3/35:58200
$Fred Fardbarkle:674-843-1385:20 Parak Lane, DeLuth, MN 23850:4/12/23:780900
fengdatia$nliang
smile of$ life
⑤ -E 筛选多个
4、文件查找命令
# which 只能查找命令
[root@yyds <sub>]# which grep
alias grep=grep --color=auto
/usr/bin/grep
# type -a 查找内置命令
[root@yyds </sub>]# type -a ls
ls is aliased to `ls --color=auto
ls is /usr/bin/ls
5、sort-字符处理命令-排序
\\## 注意:默认按照每一行的第一个字符进行排序,如果字符相同就往后推,字母按照a-z的顺序排序,排序不修改原 文件内容
#选项:
-t:指定分隔符
-k:指定按照某一列,进行排序
-n:按照阿拉伯数字排序
-r:reverse 倒叙排序
① -t 指定分割符
[root@yyds <sub>]# sort -n -t . ./moon.txt
0.2.8.122:45
1.0.0.100:45
1.00.58
2.5.3.520:87
9.4.9.789:99
112.4.1:57 185
4543.4:22
[root@yyds </sub>]# cat ./moon.txt
112.4.1:57 185
4543.4:22
1.00.58
1.0.0.100:45
2.5.3.520:87
9.4.9.789:99
0.2.8.122:45
② -k 按指定某一列,进行排序
[root@yyds ~]# sort -k 3 -n -t . ./moon.txt
4543.4:22
1.0.0.100:45
112.4.1:57 185
2.5.3.520:87
0.2.8.122:45
9.4.9.789:99
1.00.58
③ -n ,按阿拉伯数字排列
[root@yyds ~]# sort -n -t . ./xx.txt
0.2.8.122:45
0.2.8.122:45
1.00.58
1.00.58
2.5.3.520:87
2.5.3.520:87
9.4.9.789:99
9.4.9.789:99
④-r , reverse 倒叙排列
[root@yyds ~]# sort -r -t . ./xx.txt
9.4.9.789:99
9.4.9.789:99
2.5.3.520:87
2.5.3.520:87
1.00.58
1.00.58
0.2.8.122:45
0.2.8.122:45
组合用法 -t 放最后
[root@yyds ~]# sort -rnk 2 -t . ./xx.txt
2.5.3.520:87
2.5.3.520:87
9.4.9.789:99
9.4.9.789:99
0.2.8.122:45
0.2.8.122:45
1.00.58
1.00.58
以上是关于08-文件过滤排序命令grep,sort,which的主要内容,如果未能解决你的问题,请参考以下文章
正则 grep 管道符号 “|” 特殊符号“||”“&&” wc 命令 分隔符号cut 排序sort 去重复uniq 符号 ` `的使用