码迷,mamicode.com
首页 > 系统相关 > 详细

linux的cut、sort_wc_uniq、tee_tr_split命令及一些特殊符号介绍

时间:2017-11-17 21:39:28      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:linux   cut   sort   wc   uniq   tee   

shell特殊符号cut命令:


1、* 任意个任意字符(这是一个通配符)

2、? 任意一个字符

3、 # 注释字符

4、 \ 脱义字符

5、 | 管道符


几个和管道有关的命令:


1、cut 分割,-d 分隔符  -f 指定段号   -c 指定第几个字符,

示例:

[root@aminglinux-01 ~]# cat /etc/passwd |head
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@aminglinux-01 ~]# cat /etc/passwd |head -2
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@aminglinux-01 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1
root
bin
[root@aminglinux-01 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1,2
root:x
bin:x
[root@aminglinux-01 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1-3
root:x:0
bin:x:1
[root@aminglinux-01 ~]# cat /etc/passwd |head -2 |cut -c 4
t
:


sort_wc_uniq命令:


2、sort 排序, -n 以数字排序 -r 反序  -t 分隔符 -kn1/-kn1,n2

示例如下:

root@aminglinux-01 ~]# sort /etc/passwd   (按照ID号顺序排序,数字、字母等)
adm:x:3:4:adm:/var/adm:/sbin/nologin
aminglinux:x:1002:1002::/home/aminglinux:/bin/bash
aming:x:1000:1005::/home/aming:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
chrony:x:997:995::/var/lib/chrony:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin

3、 wc -l 统计行数 -m 统计字符数 -w 统计词

示例:

[root@aminglinux-01 ~]# wc -l 1.txt
22 1.txt
[root@aminglinux-01 ~]# wc -m 1.txt
450 1.txt
[root@aminglinux-01 ~]# wc -m 2.txt
0 2.txt
[root@aminglinux-01 ~]# wc -w 2.txt
0 2.txt

4、 uniq 去重, -c统计行数

单独使用uniq命令去重,只有在列表相同的上下序列才去重,示例:

[root@aminglinux-01 ~]# vim 2.txt
[root@aminglinux-01 ~]# cat 2.txt
123
abc 111,222
123
abc
1
2
1
[root@aminglinux-01 ~]# uniq 2.txt   //没有序列相同的,无法去重。
123
abc 111,222
123
abc
1
2
1
[root@aminglinux-01 ~]# vim 2.txt   //更改数字序列
[root@aminglinux-01 ~]# cat 2.txt
123
abc 111,222
123
abc
1
1
2
[root@aminglinux-01 ~]# uniq 2.txt      //去重相同的数字
123
abc 111,222
123
abc
1
2

结合sort命令使用先排序,然后去重,计算重复次数,示例:

[root@aminglinux-01 ~]# sort 2.txt         //自动排序2.txt里面文字列表
1
1
123
123
2
abc
abc 111,222
[root@aminglinux-01 ~]# sort 2.txt |uniq     //去重
1
123
2
abc
abc 111,222
[root@aminglinux-01 ~]# sort 2.txt |uniq -c  //计算重复次数
2 1
2 123
1 2
1 abc
1 abc 111,222


tee_tr_split命令:


5、 tee 和>类似,重定向的同时还在屏幕显示

示例:

[root@aminglinux-01 ~]# cat a.txt
[root@aminglinux-01 ~]# sort 2.txt |uniq -c |tee a.txt
2 1
2 123
1 2
1 abc
1 abc 111,222
[root@aminglinux-01 ~]# cat a.txt
2 1
2 123
1 2
1 abc
1 abc 111,222
[root@aminglinux-01 ~]# sort 2.txt |uniq -c |tee -a a.txt    //tee加-a是追加
2 1
2 123
1 2
1 abc
1 abc 111,222
[root@aminglinux-01 ~]# cat a.txt
2 1
2 123
1 2
1 abc
1 abc 111,222
2 1
2 123
1 2
1 abc
1 abc 111,222

6、tr 替换字符,tr ‘a‘ ‘b‘,大小写替换tr ‘[a-z]‘ ‘[A-Z]‘

示例:

[root@aminglinux-01 ~]# echo "aminglinux" |tr ‘[al]‘ ‘[AL]‘
AmingLinux
[root@aminglinux-01 ~]# echo "aminglinux" |tr ‘[a-z]‘ ‘[A-Z]‘
AMINGLINUX
[root@aminglinux-01 ~]# echo "aminglinux" |tr ‘[a-z]‘ ‘1‘
1111111111

7、 split 切割,-b大小(默认单位字节),-l行数  (切割日志,两种用法)

使用命令:

split -b 1000m bigfile
split -l   1000    bigfile

示例:

[root@aminglinux-01 test]# ls
a.txt
[root@aminglinux-01 test]# split -b 100k a.txt
[root@aminglinux-01 test]# ls
a.txt  xaa  xab  xac
[root@aminglinux-01 test]# du -sh *
256K	a.txt
100K	xaa
100K	xab
52K	xac
[root@aminglinux-01 test]# rm -f x*
[root@aminglinux-01 test]# split -b 100k a.txt abc
[root@aminglinux-01 test]# ls
abcaa  abcab  abcac  a.txt
[root@aminglinux-01 test]# split -b 100k a.txt abc.
[root@aminglinux-01 test]# ls
abcaa  abc.aa  abcab  abc.ab  abcac  abc.ac  a.txt
[root@aminglinux-01 test]# rm -f abc*
[root@aminglinux-01 test]# split -l 1000 a.txt
[root@aminglinux-01 test]# ls -l
总用量 516
-rw-r--r--. 1 root root 257421 11月 17 20:00 a.txt
-rw-r--r--. 1 root root  42094 11月 17 20:04 xaa
-rw-r--r--. 1 root root  44424 11月 17 20:04 xab
-rw-r--r--. 1 root root  40244 11月 17 20:04 xac
-rw-r--r--. 1 root root  40202 11月 17 20:04 xad
-rw-r--r--. 1 root root  34871 11月 17 20:04 xae
-rw-r--r--. 1 root root  39403 11月 17 20:04 xaf
-rw-r--r--. 1 root root  16183 11月 17 20:04 xag
[root@aminglinux-01 test]# wc -l *
6495 a.txt
1000 xaa
1000 xab
1000 xac
1000 xad
1000 xae
1000 xaf
495 xag
12990 总用量


shell特殊符号下:


1、$ 变量前缀,!$组合,正则里面表示行尾

2、;多条命令写到一行,用分号分割

[root@aminglinux-01 ~]# ls 1.txt ; wc -l 2.txt
1.txt
7 2.txt

3、 ~ 用户家目录,后面正则表达式表示匹配符

4、& 放到命令后面,会把命令丢到后台

5、> >> 2> 2>> &>


>    :正确重定向,会把之前的文件覆盖掉。

>>  :追加重定向,追加内容正确输出。

2>  :错误重定向

2>>:追加错误重定向

&>  :表示正确与错误输出重定向


6、 [ ] 指定字符中的一个,如[0-9],[a-zA-Z],[abc]

7、|| 和 && ,用于命令之间。

|| :表示或者的意思,当同时使用执行两条命令时,如果第一条命令执行不成功,那就执行第二条命令,如果第一条命令执行成功,那就不执行第二条命令,示例如下:

[root@aminglinux-01 ~]# ls 1a.txt || wc -l 2.txt
ls: 无法访问1a.txt: 没有那个文件或目录
7 2.txt
[root@aminglinux-01 ~]# ls 1.txt || wc -l 2.txt
1.txt

&&:当同时使用执行两条命令时,如果前面命令执行成功才执行后面的命令,否则不执行。

示例如下:

[root@aminglinux-01 ~]# ls 1.txt && wc -l 2.txt
1.txt
7 2.txt
[root@aminglinux-01 ~]# ls 1a.txt && wc -l 2.txt
ls: 无法访问1a.txt: 没有那个文件或目录


实验:

[root@aminglinux-01 ~]# ls
111  1_heard.txt.bak  1.txt      234    3.txt  aa.txt  anaconda-ks.cfg  test
123  1_sorft.txt.bak  1.txt.bak  2.txt  456    aming2  bb.txt           安诺云智平台介绍(PPT模板).pptx
[root@aminglinux-01 ~]# [ -d aminglinux ] || mkdir aminglinux
[root@aminglinux-01 ~]# ls
111  1_heard.txt.bak  1.txt      234    3.txt  aa.txt  aminglinux       bb.txt  安诺云智平台介绍(PPT模板).pptx
123  1_sorft.txt.bak  1.txt.bak  2.txt  456    aming2  anaconda-ks.cfg  test
[root@aminglinux-01 ~]# [ -d aminglinux ] && mkdir aminglinux
mkdir: 无法创建目录"aminglinux": 文件已存在
[root@aminglinux-01 ~]# [ -d aminglinux ] || mkdir aminglinux

解释:

[ -d aminglinux ]:判断一个目录是否存在。


本文出自 “Gary博客” 博客,请务必保留此出处http://taoxie.blog.51cto.com/10245493/1982893

linux的cut、sort_wc_uniq、tee_tr_split命令及一些特殊符号介绍

标签:linux   cut   sort   wc   uniq   tee   

原文地址:http://taoxie.blog.51cto.com/10245493/1982893

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!