8.10 shell特殊符号cut命令linux
8.11 sort_wc_uniq命令
8.12 tee_tr_split命令正则表达式
8.13 shell特殊符号下shell
相关测验题目:http://ask.apelearn.com/question/5437 扩展vim
1. source exec 区别 http://alsww.blog.51cto.com/2001924/1113112bash
2. Linux特殊符号大全http://ask.apelearn.com/question/7720less
3. sort并未按ASCII排序 http://blog.csdn.net/zenghui08/article/details/7938975工具
8.10 shell特殊符号cut命令ui
1. 特殊符号spa
* 任意个任意字符.net
*表明零个或多个任意字符
? 任意一个字符
?只表明一个任意的字符
# 注释字符
表示注释说明,即#后面的内容都会被忽略
\ 脱义字符
这个字符会将后面的特殊符号 (如*) 还原为普通字符
| 管道符
这个字符前面曾屡次出现过,它的做用是将前面命令的输出做为后面命令的输人。这里提到的后面的命令,并非全部的命令均可以的,通常针对文档操做的命令比较经常使用。例如cat、less、head, tail、grep、cut、sort、wc、uniq、tee、tr、split、sed、awk等,其中grep、sed和awk是正则表达式,必须掌握的工具
2.cut命令
cut命令用来截取某一个字段
其格式为cut –d '分隔字符' [-cf] n
-d: 后面跟分隔字符,分隔字符要用单引号括起来。
-c: 后面接的是第几个字符。
-f: 后面接的是第几个区块
-d后面加:做为分割字符,-f1表示截取第一段,-f和1之间的空格无关紧要。
[root@localhost ~]# cat /etc/passwd | cut -d ':' -f 1 | head -5
root
bin
daemon
adm
lp
-c选项后面能够是1个数字n,也能够是一个区间n1-n2,还能够是多个数字n一、n2和n3。
[root@localhost ~]# head -n2 /etc/passwd | cut -c2
o
i
[root@localhost ~]# head -n2 /etc/passwd | cut -c1
r
b
[root@localhost ~]# head -n2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@localhost ~]# head -n2 /etc/passwd | cut -c1-10
root:x:0:0
bin:x:1:1:
[root@localhost ~]# head -n2 /etc/passwd | cut -c1,3,10
ro0
bn:
[root@localhost ~]#
8.11 sort_wc_uniq命令
1. sort命令用作排序,其格式为sort [ -t 分隔符] [kn1,n2 ] [-nru],这里n1和n2指的是数字。
-t : 后面跟分隔字符,做用跟cut的-d选项同样。
-n:表示使用纯数字排序。字母和特殊符号都为0。
-r:表示反向排序。
-u:表示去重复
-kn1,n2: 表示由n1区间排序到n2区间,能够只写-kn1,即对n1字段排序。
若是sort不加任何选项,则从首字符向后依次按ASCII码值进行比较, 最后将它们按升序输出。
[root@localhost ~]# head -n5 /etc/passwd | sort
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
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]#
-t选项后面加分隔符,-k选项后面跟单个数字表示对第几个区域的字符串排序,-n选项则表示使用纯数字排序。
[root@localhost ~]# head -n5 /etc/passwd
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
[root@localhost ~]# head -n5 /etc/passwd | sort -t: -k3 -n
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
-k选项后面跟数字n1和n2表示对第n1和n2区域的字符串排序,-r表示反向排序。
[root@localhost ~]# head -n5 /etc/passwd | sort -t: -k3,5 -r
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]#
这里的-k3,5表示对第3至第5区域内的字符串排序。
2. wc命令用于统计文档的行数、字符数或词数。
经常使用选项
-l:统计行数
-m:统计字符数
-w:统计词数
不跟任何选项,直接跟文档,则会把行数、词数和字符数依次输出
[root@localhost ~]# wc /etc/passwd
21 43 1040 /etc/passwd
[root@localhost ~]# wc -l /etc/passwd
21 /etc/passwd
[root@localhost ~]# wc -m /etc/passwd
1040 /etc/passwd
[root@localhost ~]# wc -w /etc/passwd
43 /etc/passwd
[root@localhost ~]#
3. uniq命令用来删除重复的行,该命令只有-c选项比较经常使用,它表示统计重复的行数,并把行数写在前面。
[root@localhost ~]# vim testb.txt
111
222
111
333
使用uniq前,必须先给文件排序,不然无论用。
[root@localhost ~]# uniq testb.txt
111
222
111
333
[root@localhost ~]# sort testb.txt | uniq
111
222
333
[root@localhost ~]# sort testb.txt | uniq -c
2 111
1 222
1 333
[root@localhost ~]#
8.12 tee_tr_split命令
1. 命令tee后面跟文件名,做用和重定向>相似,重定向的同时还在屏幕显示,该命令经常使用于管道符 | 后。
有2层含义:先重定向,再把管道前面的结果打印在屏幕上。
[root@localhost ~]# echo "aaa" | tee testb.txt
aaa
[root@localhost ~]# cat testb.txt
aaa
清空testb.txt,就用命令>能够了
[root@localhost ~]# >testb.txt
[root@localhost ~]# cat testb.txt
[root@localhost ~]#
选项“-a“就是追加
[root@localhost ~]# vim 2.txt
111
222
111
333
[root@localhost ~]# cat testb.txt
[root@localhost ~]# sort 2.txt | uniq -c |tee -a testb.txt
2 111
1 222
1 333
[root@localhost ~]# sort 2.txt | uniq -c |tee -a testb.txt
2 111
1 222
1 333
[root@localhost ~]# cat testb.txt
2 111
1 222
1 333
2 111
1 222
1 333
[root@localhost ~]#
2. tr命令用于替换字符,经常使用来处理文档中出现的特殊符号。
该命令经常使用的选项有如下两个。
-d:表示删除某个字符,后面跟要删除的字符。
-s:表示删除重复的字符。
tr命令经常使用于把小写字母变成大写字母,如tr'[a-z]''[A-Z]'。
[root@localhost ~]# head -n2 /etc/passwd |tr '[a-z]' '[A-Z]'
ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
BIN:X:1:1:BIN:/BIN:/SBIN/NOLOGIN
[root@localhost ~]#
tr命令还能够替换一个字符;
[root@localhost ~]# grep 'root' /etc/passwd |tr 'r' 'R'
Root:x:0:0:Root:/Root:/bin/bash
opeRatoR:x:11:0:opeRatoR:/Root:/sbin/nologin
[root@localhost ~]# echo "aminglinux" |tr '[al]' '[AL]'
AmingLinux
[root@localhost ~]# echo "aminglinux" |tr '[a-z]' '1'
1111111111
[root@localhost ~]#
不过替换、删除以及去重复等操做都是针对一个字符来说的,有必定的局限性;若是针对一个字符串,就不能使用了,因此你只须要简单了解一下tr命令便可。
3. split命令用于切割文档
经常使用的选项
-b:表示依据大小来分割文档,单位为byte
-l:表示依据行数来分割文档
[root@localhost ~]# mkdir split_dir
[root@localhost ~]# cd !$
cd split_dir
[root@localhost split_dir]# cp /etc/passwd ./
[root@localhost split_dir]# split -b 500 passwd
[root@localhost split_dir]# ls
passwd xaa xab xac
[root@localhost split_dir]#
若是split不指定目标文件名,则会以xaa、xab…..这样的文件名来存取切割后的文件。固然,咱们也能够指定目标文件名。
[root@localhost split_dir]# rm -rf xa*
[root@localhost split_dir]# split -b 500 passwd 123
[root@localhost split_dir]# ls
123aa 123ab 123ac passwd
[root@localhost split_dir]#
[root@localhost split_dir]# rm -rf 123*
[root@localhost split_dir]# split -l 10 passwd
[root@localhost split_dir]# ls
passwd xaa xab xac
[root@localhost split_dir]# wc -l *
21 passwd
10 xaa
10 xab
1 xac
42 total
[root@localhost split_dir]#
8.13 shell特殊符号下
$ 变量前缀,!$组合,正则里面表示行尾
[root@localhost split_dir]# cd ..
[root@localhost ~]# ls testb.txt
testb.txt
[root@localhost ~]# ls !$
ls testb.txt
testb.txt
[root@localhost ~]#
;多条命令写到一行,用分号分割
[root@localhost ~]# mkdir testdir ; touch test1.txt ; touch test2.txt ; ls -d test*
test1.txt test2.txt testb.txt testdir
[root@localhost ~]#
~ 用户家目录,root用户的家目录是/root,普通用户则是/home/username,后面正则表达式表示匹配符
[root@localhost ~]# cd ~
[root@localhost ~]# pwd
/root
[root@localhost ~]# su aming
[aming@localhost root]$ cd ~
[aming@localhost ~]$ pwd
/home/aming
[aming@localhost ~]$
& 放到命令后面,会把命令丢到后台,它经常使用于命令运行时间较长的状况
[aming@localhost ~]$ sleep 30 &
[1] 2513
[aming@localhost ~]$ jobs
[1]+ Running sleep 30 &
[aming@localhost ~]$
重定向符号>; >>; 2>; 2>>; &>
[root@localhost ~]# ls aaaa
ls: cannot access aaaa: No such file or directory
[root@localhost ~]# ls aaaa 2> /tmp/error
[root@localhost ~]# cat !$
cat /tmp/error
ls: cannot access aaaa: No such file or directory
[root@localhost ~]# ls aaaa 2>> /tmp/error
[root@localhost ~]# cat !$
cat /tmp/error
ls: cannot access aaaa: No such file or directory
ls: cannot access aaaa: No such file or directory
[root@localhost ~]#
[ ] 为字符组合,用于指定字符中的一个,能够是一个范围[0-9],[a-zA-Z],[abc]
[root@localhost ~]# cd /tmp/10
[root@localhost 10]# ls -d test*
test1.txt test2.txt testb.txt testdir
[root@localhost 10]# ls -d test[1-3].txt
test1.txt test2.txt
[root@localhost 10]# ls -d test[12b].txt
test1.txt test2.txt testb.txt
[root@localhost 10]# ls -d test[1-9].txt
test1.txt test2.txt
[root@localhost 10]# ls -d test[1-9a-z].txt
test1.txt test2.txt testb.txt
[root@localhost 10]#
|| 和 && ,用于命令之间
command1;command2 :使用;时,无论command1是否执行成功,都会执行command2。
command1 && command2 :使用&&时,只有command1执行成功后,command2才会执行,不然command2不执行。
command1 | | command2:使用 | | 时,command1执行成功后则command2不执行,不然执行command2,即command1和command2中总有一条命令会执行。
[root@localhost 10]# rm -rf test*
[root@localhost 10]# touch test1 test3
[root@localhost 10]# ls test2 && touch test2
ls: cannot access test2: No such file or directory
[root@localhost 10]# ls test2
ls: cannot access test2: No such file or directory
[root@localhost 10]# ls test2 || touch test2
ls: cannot access test2: No such file or directory
[root@localhost 10]# ls test*
test1 test2 test3
[root@localhost 10]#
友情连接:阿铭Linux