【CentOS 7笔记35】,几个特殊符号和一些常用命令#

shallow丿ove


特殊符号

  • *任意个任意字符

  • ?任意一个字符

  • #注释字符

  • \脱义字符

  • |管道符

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

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

  • wc -l统计行数,-m统计字符数,-w统计词

  • uniq去重,-c统计行数

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

  • tr替换字符,tr 'a''b',大小写替换tr '[a-z]''[A-Z]'

  • split切割,-b大小(默认单位字节),-l行数

cut

[root@localhost ~]# cat /etc/passwd | head -2
	root:x:0:0:root:/root:/bin/bash
	bin:x:1:1:bin:/bin:/sbin/nologin
[root@localhost ~]# cat /etc/passwd | head -2 | cut -d ":" -f 1	#截取第一段,-d表示以":"分隔符
	root
	bin
[root@localhost ~]# cat /etc/passwd | head -2 | cut -d ":" -f 2	#截取第二段
	x
	x
[root@localhost ~]# cat /etc/passwd | head -2 | cut -d ":" -f 1,2	#截取第一、二段
	root:x
	bin:x
[root@localhost ~]# cat /etc/passwd | head -2 | cut -d ":" -f 1,3	#截取第一、三段
	root:0
	bin:1
[root@localhost ~]# cat /etc/passwd | head -2 | cut -d ":" -f 1-3	#截取第一到三段段
	root:x:0
	bin:x:1
[root@localhost ~]# cat /etc/passwd | head -2 | cut -c 4
	t
	:

awk

sort

按ASCII码排序

[root@localhost ~]# cat /etc/passwd | head | cut -d ":" -f 1 > 1.txt
[root@localhost ~]# cat 1.txt
	root
	bin
	daemon
	adm
	lp
	sync
	shutdown
	halt
	mail
	operator
[root@localhost ~]# sort 1.txt
	adm
	bin
	daemon
	halt
	lp
	mail
	operator
	root
	shutdown
	sync

按数字排序,其他字符视为0

[root@localhost ~]# cat /etc/passwd | head | cut -d ":" -f 3,4 > 1.txt
[root@localhost ~]# cat 1.txt
	0:0
	1:1
	2:2
	3:4
	4:7
	5:0
	6:0
	7:0
	8:12
	11:0
[root@localhost ~]# sort -n 1.txt
	0:0
	1:1
	11:0
	2:2
	3:4
	4:7
	5:0
	6:0
	7:0
	8:12

按反序排序-r

[root@localhost ~]# sort -rn 1.txt
	11:0
	8:12
	7:0
	6:0
	5:0
	4:7
	3:4
	2:2
	1:1
	0:0

分隔-t


wc

[root@localhost ~]# wc -l 1.txt
	10 1.txt


[root@localhost ~]# echo 123 > 1.txt
[root@localhost ~]# echo abc >> 1.txt
[root@localhost ~]# cat 1.txt
	123
	abc
[root@localhost ~]# wc -m 1.txt
	8 1.txt
[root@localhost ~]# cat -A 1.txt
	123$
	abc$

换行符

以空白字符分隔

uniq

[root@localhost ~]# cat 1.txt
	123
	abc
	1111111
	2222222
	1111111
	2222222
	asd
	abc

[root@localhost ~]# uniq 1.txt
	123
	abc
	1111111
	2222222
	1111111
	2222222
	asd
	abc

[root@localhost ~]# vi 1.txt
[root@localhost ~]# cat 1.txt
	123
	abc
	1111111
	1111111
	2222222
	asd
	abc

[root@localhost ~]# uniq 1.txt
	123
	abc
	1111111
	2222222
	asd
	abc

先排序再去重

[root@localhost ~]# sort 1.txt | uniq
	
	1111111
	123
	2222222
	abc
	asd

统计

[root@localhost ~]# sort 1.txt | uniq -c
	      1 
	      2 1111111
	      1 123
	      1 2222222
	      2 abc
	      1 asd

tee输出重定向

[root@localhost ~]# sort 1.txt | uniq -c | tee 2.txt
	      1 
	      2 1111111
	      1 123
	      1 2222222
	      2 abc
	      1 asd
[root@localhost ~]# cat 2.txt 
	      1 
	      2 1111111
	      1 123
	      1 2222222
	      2 abc
	      1 asd

tee -a追加输出重定向

[root@localhost ~]# sort 1.txt | uniq -c | tee -a 2.txt
	      1 
	      2 1111111
	      1 123
	      1 2222222
	      2 abc
	      1 asd
[root@localhost ~]# cat 2.txt 
	      1 
	      2 1111111
	      1 123
	      1 2222222
	      2 abc
	      1 asd
	      1 
	      2 1111111
	      1 123
	      1 2222222
	      2 abc
	      1 asd

tr命令

[root@localhost ~]# echo "helloworld" | tr 'h' 'H'
	Helloworld
[root@localhost ~]# echo "helloworld" | tr '[hw]' '[HW]'
	HelloWorld
[root@localhost ~]# echo "helloworld" | tr '[a-z]' '[A-Z]'
	HELLOWORLD

切割命令split

针对大小-b,不加单位默认字节

[root@localhost ~]# find /etc/ -type f -name "*conf" -exec cat {} >> 1.txt \;
[root@localhost ~]# du -sh 1.txt
	224K	1.txt
[root@localhost ~]# mv 1.txt 111/
[root@localhost ~]# cd 111
[root@localhost 111]# ls
	1.txt

不指定

[root@localhost 111]# split -b 1000 1.txt
[root@localhost 111]# ls
	1.txt  xal  xax  xbj  xbv  xch  xct  xdf  xdr  xed  xep  xfb  xfn  xfz  xgl  xgx  xhj  xhv  xih
	xaa    xam  xay  xbk  xbw  xci  xcu  xdg  xds  xee  xeq  xfc  xfo  xga  xgm  xgy  xhk  xhw  xii
	xab    xan  xaz  xbl  xbx  xcj  xcv  xdh  xdt  xef  xer  xfd  xfp  xgb  xgn  xgz  xhl  xhx  xij
	xac    xao  xba  xbm  xby  xck  xcw  xdi  xdu  xeg  xes  xfe  xfq  xgc  xgo  xha  xhm  xhy  xik
	xad    xap  xbb  xbn  xbz  xcl  xcx  xdj  xdv  xeh  xet  xff  xfr  xgd  xgp  xhb  xhn  xhz  xil
	xae    xaq  xbc  xbo  xca  xcm  xcy  xdk  xdw  xei  xeu  xfg  xfs  xge  xgq  xhc  xho  xia  xim
	xaf    xar  xbd  xbp  xcb  xcn  xcz  xdl  xdx  xej  xev  xfh  xft  xgf  xgr  xhd  xhp  xib  xin
	xag    xas  xbe  xbq  xcc  xco  xda  xdm  xdy  xek  xew  xfi  xfu  xgg  xgs  xhe  xhq  xic  xio
	xah    xat  xbf  xbr  xcd  xcp  xdb  xdn  xdz  xel  xex  xfj  xfv  xgh  xgt  xhf  xhr  xid  xip
	xai    xau  xbg  xbs  xce  xcq  xdc  xdo  xea  xem  xey  xfk  xfw  xgi  xgu  xhg  xhs  xie  xiq
	xaj    xav  xbh  xbt  xcf  xcr  xdd  xdp  xeb  xen  xez  xfl  xfx  xgj  xgv  xhh  xht  xif  xir
	xak    xaw  xbi  xbu  xcg  xcs  xde  xdq  xec  xeo  xfa  xfm  xfy  xgk  xgw  xhi  xhu  xig  xis
[root@localhost 111]# rm -f x*
[root@localhost 111]# ls
	1.txt
[root@localhost 111]# split -b 100K 1.txt
[root@localhost 111]# ls
	1.txt  xaa  xab  xac
[root@localhost 111]# du -sh *
	224K	1.txt
	100K	xaa
	100K	xab
	24K	xac

指定

[root@localhost 111]# rm -f x*
[root@localhost 111]# split -b 100K 1.txt 1-
[root@localhost 111]# ls
	1-aa  1-ab  1-ac  1.txt

针对行-l

[root@localhost 111]# rm -f *x
[root@localhost 111]# wc -l 1.txt 
	5736 1.txt
[root@localhost 111]# split -l 1000 1.txt 1-
[root@localhost 111]# ls -l
	total 452
	-rw-r--r--. 1 root root  44490 Nov 17 07:20 1-aa
	-rw-r--r--. 1 root root  44233 Nov 17 07:20 1-ab
	-rw-r--r--. 1 root root  43363 Nov 17 07:20 1-ac
	-rw-r--r--. 1 root root  36299 Nov 17 07:20 1-ad
	-rw-r--r--. 1 root root  35017 Nov 17 07:20 1-ae
	-rw-r--r--. 1 root root  22988 Nov 17 07:20 1-af
	-rw-r--r--. 1 root root 226390 Nov 17 07:13 1.txt

特殊符号

[root@localhost ~]# for i in `seq 1 10`
	> do
	> echo $i
	> done
	1
	2
	3
	4
	5
	6
	7
	8
	9
	10
[root@localhost ~]# for i in `seq 1 10`; do echo $i; done
	1
	2
	3
	4
	5
	6
	7
	8
	9
	10
[root@localhost ~]# ls 111 ; wc -l 1.txt 
	1-aa  1-ab  1-ac  1-ad  1-ae  1-af  1.txt
	5736 1.txt

~用户家目录,而在正则表达式中表示匹配符

&放在命令后面,指将命令丢到后台

[root@localhost ~]# ls 111 || wc -l 1.txt 	#或
	1-aa  1-ab  1-ac  1-ad  1-ae  1-af  1.txt
[root@localhost ~]# ls 1111 || wc -l 1.txt 
	ls: cannot access 1111: No such file or directory
	5736 1.txt
[root@localhost ~]# ls 111 && wc -l 1.txt 	#与
	1-aa  1-ab  1-ac  1-ad  1-ae  1-af  1.txt
	5736 1.txt
[root@localhost ~]# ls 1111 && wc -l 1.txt 
	ls: cannot access 1111: No such file or directory
[root@localhost ~]# [ -d hello ] || mkdir hello
[root@localhost ~]# ls
	hello  anaconda-ks.cfg
[root@localhost ~]# [ -d hello ] && mkdir hello
	mkdir: cannot create directory ‘hello’: File exists
[root@localhost ~]# [ -d hello ] || mkdir hello

转载于:https://my.oschina.net/u/3892756/blog/3056528

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值