0404课的预习任务

8.10 shell特殊符 cut命令

特殊符号

*                 任意个任意字符

?                 任意一个字符     ?.txt

#                 注释字符 不生效,说明文字

\                 脱义字符 用在符号前

|                 管道符


cut           截取字符串

                -d 指定分隔符

                -f 指定选取第几段

                -c 指定选取第几个字符(-c 和 -d 是两种类型,不能共用)

[root@arslinux-01 ~]# cat /etc/passwd |head -2|cut -d : -f 1
root
bin
[root@arslinux-01 ~]# cat /etc/passwd |head -2|cut -d : -f 1,2
root:x
bin:x
[root@arslinux-01 ~]# cat /etc/passwd |head -2|cut -d : -f 1-3
root:x:0
bin:x:1
[root@arslinux-01 ~]# cat /etc/passwd |head -2|cut -c 4
t
:


8.11 sort wc uniq命令

sort文件名        排序(默认按照ACSII排序)

                -n 以数字排序

                -r 反序

                -t 分隔符

                -k 是指定针对第几段排序 -nk 1,-nk 1,2


sort     默认用法

[root@arslinux-01 ~]# sort 1.txt
<
>
]
{
1.txt
222222aaaaaaaaa
22333333
22aaa
231312131
2.txt
4888888888888888888adslkfj;a
adm:x:3:4:adm:/var/adm:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
*slkdf
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin


sort -n 以数字顺序排序,特殊符号和字母被视为0

默认排序是按照 空字符 - 特殊符号 - 数字 - 字母

[root@arslinux-01 ~]# sort -n 1.txt
<
>
]
{
adm:x:3:4:adm:/var/adm:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
*slkdf
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
1.txt
2.txt
22aaa
222222aaaaaaaaa
22333333
231312131
4888888888888888888adslkfj;a


sort -r    反向排序(和 sort 相反)

[root@arslinux-01 ~]# sort -r 1.txt
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
*slkdf
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
4888888888888888888adslkfj;a
2.txt
231312131
22aaa
22333333
222222aaaaaaaaa
1.txt
{
]
>
<


wc     统计

            -l 统计行数

            -m 统计字符数(包括隐藏的换行符,cat -A 可查看全部)

            -w 统计词

[root@arslinux-01 ~]# wc -l 1.txt
20 1.txt
[root@arslinux-01 ~]# wc -m 1.txt
425 1.txt
[root@arslinux-01 ~]# wc -w 1.txt
21 1.txt


uniq     去重(一般先排序在去重)

            -c 统计重复的次数(在每列旁边显示该行重复出现的次数

[root@arslinux-01 ~]# sort -n 2.txt
abc
abc 1111,2222
1
1
2
123
123
[root@arslinux-01 ~]# sort -n 2.txt |uniq
abc
abc 1111,2222
1
2
123
[root@arslinux-01 ~]# sort -n 2.txt |uniq -c
1 abc
1 abc 1111,2222
2 1
1 2
2 123

资料:http://man.linuxde.net/sort


8.12 tee tr split命令

tee > 类似,重定向的同时还在屏幕显示,而 > 不显示过程

[root@arslinux-01 ~]# sort 2.txt | uniq -c > a.txt
[root@arslinux-01 ~]# cat !$
cat a.txt
2 1
2 123
1 2
1 abc
1 abc 1111,2222
[root@arslinux-01 ~]# sort 2.txt | uniq -c |tee b.txt
2 1
2 123
1 2
1 abc
1 abc 1111,2222


>文件名 清空文件内容

[root@arslinux-01 ~]# >a.txt
[root@arslinux-01 ~]# cat a.txt
[root@arslinux-01 ~]#


tee -a 和 >> 类似,追加的同时还在屏幕显示,而 >>不显示过程

[root@arslinux-01 ~]# sort 2.txt | uniq -c |tee -a a.txt
2 1
2 123
1 2
1 abc
1 abc 1111,2222
[root@arslinux-01 ~]# sort 2.txt | uniq -c |tee -a a.txt
2 1
2 123
1 2
1 abc
1 abc 1111,2222
[root@arslinux-01 ~]# cat a.txt
2 1
2 123
1 2
1 abc
1 abc 1111,2222
2 1
2 123
1 2
1 abc
1 abc 1111,2222


tr 替换字符:tr 'a' 'b'           (tr=translate)

[root@arslinux-01 ~]# echo arslinux | tr '[al]' '[AL]'
ArsLinux
[root@arslinux-01 ~]# echo arslinux | tr 'al' 'AL'
ArsLinux
[root@arslinux-01 ~]# echo arslinux | tr '[a-z]' '[A-Z]'
ARSLINUX
[root@arslinux-01 ~]# echo arslinux | tr '[a-z]' '1'
11111111
[root@arslinux-01 ~]# echo arslinux | tr '[a-z]' '[1]'
1]]]]]]]

★单个数字或字母不要用[ ]括起来,会出错


split    切割           (文件过大,切割方便操作)

        -b 按大小切割(默认单位字节,如果不写单位,就会按100B切割)

        -l 按行数切割


split -b 大小 文件名        按大小切割

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


指定切割出文件的前缀 (切割后前缀为abc后面根据aa,ab...ba,bb,...排列)

[root@arslinux-01 test]# split -b 100k a.txt abc
[root@arslinux-01 test]# ls
abcaa  abcab  abcac  a.txt
[root@arslinux-01 test]# split -b 100k a.txt abc.
[root@arslinux-01 test]# ls
abcaa  abc.aa  abcab  abc.ab  abcac  abc.ac  a.txt
[root@arslinux-01 test]# rm -f abc*


split -l 行数 文件名         按行来切割

[root@arslinux-01 test]# split -l 1000 a.txt
[root@arslinux-01 test]# ll
总用量 440
-rw-r--r--. 1 root root 217745 4月   3 22:08 a.txt
-rw-r--r--. 1 root root  33572 4月   3 22:19 xaa
-rw-r--r--. 1 root root  46174 4月   3 22:19 xab
-rw-r--r--. 1 root root  44813 4月   3 22:19 xac
-rw-r--r--. 1 root root  44010 4月   3 22:19 xad
-rw-r--r--. 1 root root  33681 4月   3 22:19 xae
-rw-r--r--. 1 root root  15495 4月   3 22:19 xaf
[root@arslinux-01 test]# wc -l *
5491 a.txt
1000 xaa
1000 xab
1000 xac
1000 xad
1000 xae
491 xaf
10982 总用量


8.13 shell特殊符号(下)

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

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

[root@arslinux-01 test]# for i in `seq 1 10`
> do
> echo $i
> done
1
2
3
4
5
6
7
8
9
10
[root@arslinux-01 test]# for i in `seq 1 10`; do echo $i; done
1
2
3
4
5
6
7
8
9
10
[root@arslinux-01 test]# ls a.txt;wc -l a.txt
a.txt
5491 a.txt


特殊符号:

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

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

>                 正确重定向,覆盖源文件

>>                追加正确重定向

2>                 错误重定向

2>>               追加错误重定向

&>                 正确错误全部重定向到一个文件

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

|| 和 &&         用于命令之间(或与和)判断


|| 第一条执行不成功就会执行第二条,第一条执行成功就不会执行第二条命令

&& 第一条执行成功才会执行第二条,第一条执行不成功就不会执行第二条

[root@arslinux-01 test]# ls la.txt || wc -l a.txt
ls: 无法访问la.txt: 没有那个文件或目录
5491 a.txt
[root@arslinux-01 test]# ls a.txt || wc -l a.txt
a.txt
[root@arslinux-01 test]# ls la.txt && wc -l a.txt
ls: 无法访问la.txt: 没有那个文件或目录
[root@arslinux-01 test]# ls a.txt && wc -l a.txt
a.txt
5491 a.txt
[root@arslinux-01 ~]# [ -d arslinux ] || mkdir arslinux
[root@arslinux-01 ~]# [ -d arslinux ] && mkdir arslinux
mkdir: 无法创建目录"arslinux": 文件已存在

|| 存在则不执行,不存在则执行

&& 存在则执行,不存在则不执行

简易审计系统(必须要预习): http://www.68idc.cn/help/server/linux/2014042190951.html


扩展

关于PROMPT_COMMAND环境变量的含义   http://www.linuxnote.org/prompt_command-environment-variables.html

source exec 区别 http://alsww.blog.51cto.com/2001924/1113112

sort并未按ASCII排序 http://blog.csdn.net/zenghui08/article/details/7938975








猜你喜欢

转载自blog.51cto.com/11530642/2374441