第八章第二节课预习
8.10 shell特殊符号cut命令
8.11 sort_wc_uniq命令
8.12 tee_tr_split命令
8.13 shell特殊符号下
相关测验题目(必须要做):http://ask.apelearn.com/question/5437
简易审计系统(必须要预习): 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
Linux特殊符号大全http://ask.apelearn.com/question/7720
sort并未按ASCII排序 http://blog.csdn.net/zenghui08/article/details/7938975
8.10 shell特殊符_cut命令

[root@linux02 ~]# ls -l 1.txt
-rw-r--r--. 1 root root 25 8月 19 17:32 1.txt
[root@linux02 ~]# #ls -l 1.txt 注释掉不生效了
可以用脱义也可以用单引号
[root@linux02 ~]# a=1
[root@linux02 ~]# b=2
[root@linux02 ~]# c=$a$b
[root@linux02 ~]# echo $c
12
[root@linux02 ~]# c='$a$b'
[root@linux02 ~]# echo $c
$a$b
[root@linux02 ~]# c=$a$b
[root@linux02 ~]# echo $c
12
[root@linux02 ~]# c=\$a\$b
[root@linux02 ~]# echo $c
$a$b

[root@linux02 ~]# cat /etc/passwd |head -2
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@linux02 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1
root
bin
[root@linux02 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1,2
root:x
bin:x
[root@linux02 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1-3
root:x:0
bin:x:1
8.11 sort_wc_uniq命令

知道第几个字符
[root@linux02 ~]# cat /etc/passwd |head -2 |cut -c 5
:
x
[root@linux02 ~]# cat /etc/passwd |head -2 |cut -c 6
x
:
[root@linux02 ~]# cat /etc/passwd |head -2 |cut -c 2
o
i
[root@linux02 ~]# sort /etc/passwd 它是是asc码排序的
adm:x:3:4:adm:/var/adm:/sbin/nologin
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
halt:x:7:0:halt:/sbin:/sbin/halt
[root@linux02 ~]# head /etc/passwd>1.txt
[root@linux02 ~]# vi 1.txt 加几行
operator:x:11:0:operator:/root:/sbin/nologin
2222222222222222222
333333333333333333
*fdlfjdlfjdlfj
_fdljfldjfldjljljlfdj
[root@linux02 ~]# sort 1.txt
<<<<<
>>>>>>
2222222222222222222
333333333333333333
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
*fdlfjdlfjdlfj
_fdljfldjfldjljljlfdj
halt:x:7:0:halt:/sbin:/sbin/halt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@linux02 ~]# sort -n 1.txt 以数字排序,除了数字其他的都认为是0
<<<<<
>>>>>>
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
*fdlfjdlfjdlfj
_fdljfldjfldjljljlfdj
halt:x:7:0:halt:/sbin:/sbin/halt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sync:x:5:0:sync:/sbin:/bin/sync
333333333333333333
2222222222222222222
[root@linux02 ~]# sort -nr 1.txt 反续排列 ,-t分隔符用的不多
[root@linux02 ~]# vi 5.txt
111
111
[root@linux02 ~]# wc -l 1.txt
16 1.txt
[root@linux02 ~]# wc -m 5.txt 明明写了6个字符因为有两个换行符
8 5.txt
[root@linux02 ~]# wc -w 5.txt 以空格字符作为分隔符,是词量
2 5.txt
去重复,现在我们
[root@linux02 ~]# vi 5.txt
111
111
bbb
bbb
3
6
9
[root@linux02 ~]# uniq 5.txt
111
bbb
3
6
9
然后不按顺序
[root@linux02 ~]# vi 5.txt
111
111
bbb
bbb
3
6
9
bbb
[root@linux02 ~]# uniq 5.txt
111
bbb
3
6
9
bbb
所以uniq是有条件的, 必须和排序一起用
[root@linux02 ~]# sort 5.txt |uniq
111
3
6
9
bbb
[root@linux02 ~]# sort 5.txt |uniq -c 还可以统计重复的次数
1
2 111
1 3
1 6
1 9
3 bbb
8.12 tee_tr_split命令
上面所有命令sort unique wc等会对文件有一定操作,但不会更改内容
[root@linux02 ~]# sort 5.txt |uniq -c >a.txt 我们并不知道 还需要看下
[root@linux02 ~]# cat a.txt
1
2 111
1 3
1 6
1 9
3 bbb
[root@linux02 ~]# >a.txt 把a清空
[root@linux02 ~]# cat a.txt
[root@linux02 ~]# sort 5.txt |uniq -c |tee a.txt (tee两次含义,一次是重定向,一个是打印在屏幕上)
1
2 111
1 3
1 6
1 9
3 bbb
[root@linux02 ~]# cat a.txt
1
2 111
1 3
1 6
1 9
3 bbb
[root@linux02 ~]# sort 5.txt |uniq -c |tee -a a.txt (-a追加 )
1
2 111
1 3
1 6
1 9
3 bbb
[root@linux02 ~]# cat a.txt
1
2 111
1 3
1 6
1 9
3 bbb
1
2 111
1 3
1 6
1 9
3 bbb
[root@linux02 ~]# echo "zhang" |tr '[za]' '[ZA]' 替换
ZhAng
[root@linux02 ~]# echo "zhang" |tr 'h' 'H'
zHang
[root@linux02 ~]# echo "zhang" |tr '[a-z]' '[A-Z]'
ZHANG
split 切割 大日志切割
[root@linux02 ~]# split -b 100m wenjian^C 100m一份切割
[root@linux02 ~]# split -l 1000 wenjian 1000行一份切割
[root@linux02 ~]# find /etc/ -type f -name "*conf" -exec cat {} >> a.txt \;
[root@linux02 ~]# du -sh a.txt
256K a.txt
[root@linux02 ~]# mkdir test
[root@linux02 ~]# mv a.txt test/
[root@linux02 ~]# cd test
[root@linux02 test]# split -b 1000 a.txt
[root@linux02 test]# ls
a.txt xar xbj xcb xct xdl xed xev xfn xgf xgx xhp xih xiz xjr
xaa xas xbk xcc xcu xdm xee xew xfo xgg xgy xhq xii xja xjs
xab xat xbl xcd xcv xdn xef xex xfp xgh xgz xhr xij xjb xjt
[root@linux02 test]# du -sh xaa
4.0K xaa
[root@linux02 test]# du -sb xaa
1000 xaa
[root@linux02 test]# rm -f x*
[root@linux02 test]# ls
a.txt
[root@linux02 test]# split -b 100k a.txt 定义大小
[root@linux02 test]# ls
a.txt xaa xab xac
[root@linux02 test]# du -sh *
256K a.txt
100K xaa
100K xab
52K xac
[root@linux02 test]# rm -f x*
[root@linux02 test]# split -b 100k a.txt abc 可以定义姓名
[root@linux02 test]# ls
abcaa abcab abcac a.txt
[root@linux02 test]# rm -f x*
[root@linux02 test]# split -b 100k a.txt abc
[root@linux02 test]# ls
abcaa abcab abcac a.txt
[root@linux02 test]# rm -f abc*
[root@linux02 test]# ls
a.txt
[root@linux02 test]# split -l 1000 a.txt 指定1000行
[root@linux02 test]# ls -l
总用量 512
-rw-r--r--. 1 root root 256191 8月 19 22:17 a.txt
-rw-r--r--. 1 root root 43712 8月 19 22:23 xaa
-rw-r--r--. 1 root root 44074 8月 19 22:23 xab
-rw-r--r--. 1 root root 38964 8月 19 22:23 xac
-rw-r--r--. 1 root root 39869 8月 19 22:23 xad
-rw-r--r--. 1 root root 35044 8月 19 22:23 xae
-rw-r--r--. 1 root root 39344 8月 19 22:23 xaf
-rw-r--r--. 1 root root 15184 8月 19 22:23 xag
[root@linux02 test]# wc -l
^C
[root@linux02 test]# wc -l *
6478 a.txt
1000 xaa
1000 xab
1000 xac
1000 xad
1000 xae
1000 xaf
478 xag
12956 总用量
8.13 shell特殊符号(下)

[root@linux02 test]# for i in `seq 1 10`;do echo $i;done 分号作用
1
2
3
4
5
6
7
8
9
10
[root@linux02 test]# ls a.txt ; wc -l a.txt
a.txt
[root@linux02 test]# ls a.txt;wc -l a.txt 分号前后可加可不加空格
a.txt
6478 a.txt
第五条最后&这个字符可以把正确错误的一起输入到一个文件里
[root@linux02 test]# ls a.txt || wc -l a.txt 第一个成功了第二个就不执行了,
a.txt
[root@linux02 test]# ls a.txt && wc -l a.txt 第一个成功 了才会执行第二个
a.txt
6478 a.txt
[root@linux02 test]# ls c.txt && wc -l a.txt 前面不成功后面也不会执行
ls: 无法访问c.txt: 没有那个文件或目录
[root@linux02 ~]# [ -d zhang ] || mkdir zhang 判断目录zhang是否存在,没存在就创建
[root@linux02 ~]# ls
1.txt 2.txt 5.txt anaconda-ks.cfg a.txt; b.txt c.txt err test zhang
[root@linux02 ~]# [ -d zhang ] && mkdir zhang
mkdir: 无法创建目录"zhang": 文件已存在

浙公网安备 33010602011771号