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

8.10 shell特殊符号cut命令 8.11 sort_wc_uniq命令 8.12 tee_t

时间:2018-01-15 20:28:23      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:回车   不执行   重复   没有   实验   ges   字符串   文件写入   bash   

8.10 shell特殊符号cut命令

技术分享图片

命令 : cut

用来截取某一个字段

语法: cut -d ‘分隔字符‘ [-cf] n 这里的n是数字

-d :后面跟分隔字符,分隔字符要用单引号括起来

-c :后面接的是第几个字符

-f :后面接的是第几个区块

[root@localhost ~]# cat /etc/passwd |cut -d ‘:‘ -f 1 |head -n5
root
bin
daemon
adm
lp
-d 后面跟分隔字符,这里使用冒号作为分割字符,-f 1 就是截取第一段,-f和1之间的空格可有可无。

[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|cut -c1-10
root:x:0:0
bin:x:1:1:
[root@localhost ~]# head -n2 /etc/passwd|cut -c5-10
:x:0:0
x:1:1:
-c 后面可以是1个数字n,也可以是一个区间n1-n2,还可以是多个数字n1,n2,n3

[root@localhost ~]# head -n2 /etc/passwd|cut -c1,3,10
ro0
bn:
技术分享图片

技术分享图片

8.11 sort_wc_uniq命令

命令 : sort

sort 用做排序

语法: sort [-t 分隔符] [-kn1,n2] [-nru] 这里的n1 < n2

-t 分隔符 :作用跟cut的-d一个意思

-n :使用纯数字排序

-r :反向排序

-u :去重复

-kn1,n2 :由n1区间排序到n2区间,可以只写-kn1,即对n1字段排序

[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
如果sort不加任何选项,则从首字符向后,依次按ASCII码值进行比较,最后将他们按升序输出。

[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
-t 后面跟分隔符,-k后面跟数字,表示对第几个区域的字符串排序,-n 则表示使用纯数字排序

[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
-k3,5 表示从第3到第5区域间的字符串排序,-r表示反向排序

技术分享图片
出现错误提示,原因是t:后面没有加空格。
技术分享图片

命令 : wc

用于统计文档的行数、字符数、词数,常用的选项为:

-l :统计行数

-m :统计字符数

-w :统计词数

[root@localhost ~]# wc /etc/passwd
27 37 1220 /etc/passwd
[root@localhost ~]# wc -l /etc/passwd
27 /etc/passwd
[root@localhost ~]# wc -m /etc/passwd
1220 /etc/passwd
[root@localhost ~]# wc -w /etc/passwd
37 /etc/passwd
wc 不跟任何选项,直接跟文档,则会把行数、词数、字符数依次输出。

技术分享图片
命令 : uniq

去重复的行,阿铭最常用的选项只有一个:

-c :统计重复的行数,并把行数写在前面

[root@localhost ~]# vim testb.txt
把下面的内容写入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
技术分享图片

8.12 tee_tr_split命令

命令 : tee

后跟文件名,类似与重定向 “>”, 但是比重定向多了一个功能,在把文件写入后面所跟的文件中的同时,还显示在屏幕上。

[root@localhost ~]# echo "aaaaaaaaaaaaaaaaaaaaaaaaaaa" |tee testb.txt
aaaaaaaaaaaaaaaaaaaaaaaaaaa
[root@localhost ~]# cat testb.txt
aaaaaaaaaaaaaaaaaaaaaaaaaaa
tee 常用语管道符 “|” 后。
技术分享图片

命令 : tr

替换字符,常用来处理文档中出现的特殊符号,如DOS文档中出现的^M符号。常用的选项有两个:

-d :删除某个字符,-d 后面跟要删除的字符

-s :把重复的字符去掉

最常用的就是把小写变大写: 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 ~]# grep ‘root‘ /etc/passwd |tr ‘r‘ ‘R‘
Root:x:0:0:Root:/Root:/bin/bash
opeRatoR:x:11:0:opeRatoR:/Root:/sbin/nologin
不过替换、删除以及去重复都是针对一个字符来讲的,有一定局限性。如果是针对一个字符串就不再管用了,所以阿铭建议你只需简单了解这个tr即可,以后你还会学到更多可以实现针对字符串操作的工具。

技术分享图片
命令 : split

切割文档,常用选项:

-b :依据大小来分割文档,单位为byte

[root@localhost ~]# mkdir split_dir
[root@localhost ~]# cd !$
cd split_dir
[root@localhost split_dir]# cp /etc/passwd ./
[root@localhost split_dir]# split -b500 passwd
[root@localhost split_dir]# ls
passwd xaa xab xac
如果split不指定目标文件名,则会以xaa xab... 这样的文件名来存取切割后的文件。当然我们也可以指定目标文件名:

[root@localhost split_dir]# split -b500 passwd 123
[root@localhost split_dir]# ls
123aa 123ab 123ac passwd
-l :依据行数来分割文档

技术分享图片

8.13 shell特殊符号下

技术分享图片

$ 除了用于变量前面的标识符外,还有一个妙用,就是和 ‘!’ 结合起来使用。
[root@localhost ~]# ls testb.txt
testb.txt
[root@localhost ~]# ls !$
ls testb.txt
testb.txt
‘!$’ 表示上条命中中最后一个变量(总之就是上条命令中最后出现的那个东西)例如上边命令最后是testb.txt那么在当前命令下输入!$则代表testb.txt.

; : 分号。平时我们都是在一行中敲一个命令,然后回车就运行了,那么想在一行中运行两个或两个以上的命令如何呢?则需要在命令之间加一个 ”;” 了。
[root@localhost ~]# ls -d test; touch test111; ls -d test
test test1 test2 test3 testa testb.txt
test test1 test111 test2 test3 testa testb.txt
~ : 用户的家目录,如果是root则是 /root ,普通用户则是 /home/username
[root@localhost ~]# cd ~
[root@localhost ~]# pwd
/root
[root@localhost ~]# su test
[test@localhost root]$ cd ~
[test@localhost ~]$ pwd
/home/test
技术分享图片
& : 如果想把一条命令放到后台执行的话,则需要加上这个符号。通常用于命令运行时间非常长的情况。
[root@localhost ~]# sleep 30 &
[1] 3260
[root@localhost ~]# jobs
[1]+ Running sleep 30 &

, >>, 2>, 2>> 前面讲过重定向符号> 以及>> 分别表示取代和追加的意思,然后还有两个符号就是这里的2> 和 2>> 分别表示错误重定向和错误追加重定向,当我们运行一个命令报错时,报错信息会输出到当前的屏幕,如果想重定向到一个文本里,则要用2>或者2>>
[root@localhost ~]# ls aaaa
ls: 无法访问aaaa: 没有那个文件或目录
[1]+ Done sleep 30
[root@localhost ~]# ls aaaa
ls: 无法访问aaaa: 没有那个文件或目录
[root@localhost ~]# ls aaaa 2> /tmp/error
[root@localhost ~]# cat /tmp/error
ls: 无法访问aaaa: 没有那个文件或目录
[root@localhost ~]# ls aaaa 2>> /tmp/error
[root@localhost ~]# cat /tmp/error
ls: 无法访问aaaa: 没有那个文件或目录
ls: 无法访问aaaa: 没有那个文件或目录
[ ] 中括号,中间为字符组合,代表中间字符中的任意一个。
[root@localhost ~]# ls -d test*
test test1 test111 test2 test3 testa testb.txt
[root@localhost ~]# ls -d test[1-3]
test1 test2 test3
[root@localhost ~]# ls -d test[1a3]
test1 test3 testa
[root@localhost ~]# ls -d test[0-9]
test1 test2 test3
[root@localhost ~]# ls -d test[0-9a-z]
test1 test2 test3 testa
&& 与 ||
在上面刚刚提到了分号,用于多条命令间的分隔符。另外还有两个可以用于多条命令中间的特殊符号,那就是 “&&” 和 “||” 下面阿铭把这几种情况全列出:

command1 ; command2
command1 && command2
command1 || command2
使用 ”;” 时,不管command1是否执行成功都会执行command2;

使用 “&&” 时,只有command1执行成功后,command2才会执行,否则command2不执行;

使用 “||” 时,command1执行成功后command2 不执行,否则去执行command2,总之command1和command2总有一条命令会执行。

在做实验前,阿铭想把所有的 test* 删除掉,可是删除的时候,却提示说权限不够,下面是阿铭排除问题的过程:

[root@localhost ~]# rm -rf test
rm: 无法删除"test2/test1": 权限不够
rm: 无法删除"test2/test3": 权限不够
rm: 无法删除"test2/test4": 权限不够
[root@localhost ~]# ls test

test1 test3 test4
[root@localhost ~]# lsattr test
-----a-------e- test2/test1
----i--------e- test2/test3
-------------e- test2/test4
[root@localhost ~]# chattr -a test2/test1
[root@localhost ~]# chattr -i test2/test3
[root@localhost ~]# rm -rf test

rm: 无法删除"test2/test1": 权限不够
rm: 无法删除"test2/test3": 权限不够
rm: 无法删除"test2/test4": 权限不够
[root@localhost ~]# ls test
test1 test3 test4
[root@localhost ~]# ls -ld test

drwxrwxr-x 2 root root 4096 5月 10 10:12 test2
[root@localhost ~]# ls -l test2/
-rw-r--r-- 1 root root 6 5月 10 10:20 test2/test1
-rw-r--r-- 1 root root 0 5月 10 10:11 test2/test3
-rw-r--r-- 1 root root 0 5月 10 10:12 test2/test4
[root@localhost ~]# lsattr test2/

-------------e- test2/test1
-------------e- test2/test3
-------------e- test2/test4
[root@localhost ~]# lsattr test2
-------------e- test2/test1
-------------e- test2/test3
-------------e- test2/test4
[root@localhost ~]# lsattr -d test2
----i--------e- test2
[root@localhost ~]# chattr -i test2/
[root@localhost ~]# rm -rf test2/
如果你之前跟着阿铭做过同样的实验,相信你也会出现同样的问题的。接下来阿铭要通过做实验来说明 “&&” 与 “||” 这两个特殊符号的作用:

[root@localhost ~]# touch test1 test3
[root@localhost ~]# ls test2 && touch test2
ls: 无法访问test2: 没有那个文件或目录
[root@localhost ~]# ls test2
ls: 无法访问test2: 没有那个文件或目录
[root@localhost ~]# ls test2 || touch test2
ls: 无法访问test2: 没有那个文件或目录
[root@localhost ~]# ls test*
test1 test2 test3

8.10 shell特殊符号cut命令 8.11 sort_wc_uniq命令 8.12 tee_t

标签:回车   不执行   重复   没有   实验   ges   字符串   文件写入   bash   

原文地址:http://blog.51cto.com/12058686/2061226

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