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

22.Shell特殊符号和cut,sort,wc,uniq,tee,tr,split命令

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

标签:就是   slim   blog   分割   题目   shell   du -sh   /etc/   错误   

五周第五次课(1月12日)

8.10 shell特殊符号cut命令

8.11 sort_wc_uniq命令

8.12 tee_tr_split命令

8.13 shell特殊符号下

相关测验题目:http://ask.apelearn.com/question/5437

扩展

  1. source exec 区别 http://alsww.blog.51cto.com/2001924/1113112
  2. Linux特殊符号大全http://ask.apelearn.com/question/7720
  3. sort并未按ASCII排序 http://blog.csdn.net/zenghui08/article/details/7938975

一 特殊符号

  • [ ] * 任意个任意字符,通配符

  • [ ] ? 任意一个字符

  • [ ] # 注释字符

  • [ ] \ 脱义字符

  • [ ] | 管道符
oot@localhost:~# a=1
root@localhost:~# b=2
root@localhost:~# c=$a$b
root@localhost:~# echo $c
12
root@localhost:~# c=\$a\$b
root@localhost:~# echo $c
$a$b

二 几个和管道有关的命令

  • [ ] cut 分割,-d 分隔符 -f 指定段号 -c 指定第几个字
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 //只取第一段
root
bin
root@localhost:~# cat /etc/passwd |head -2 |cut -d ":" -f 1,2 //取第一段,第二段
root:x
bin:x
root@localhost:~# cat /etc/passwd |head -2 |cut -c 4
t
:
  • [ ] sort 排序, -n 以数字排序 -r 反序 -t 分隔符 -kn1/-kn1,n2
    技术分享图片
root@localhost:~# sort -r /etc/passwd
xavi:x:1000:1000:xavi,xavi‘s office,62580558,62589906:/home/xavi:/bin/bash
xavidsf:x:1001:1001:xavi:/home/xavidsf:/bin/bash
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
unbound:x:993:991:Unbound DNS resolver:/etc/unbound:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
test:x:1002:1002::/home/test:/bin/bash
  • [ ] wc -l 统计行数 -m 统计字符数 -w 统计词
root@localhost:~# vi 2.txt
root@localhost:~# cat 2.txt
2221dedd
dede
ded
1213
32e4
ded
root@localhost:~# wc -l 2.txt
6 2.txt
root@localhost:~# wc -m 2.txt
32 2.txt
root@localhost:~# wc -w 2.txt
6 2.txt
  • [ ] uniq 去重, -c统计行数,文件内容是没有改变的

技术分享图片

root@localhost:~# sort 2.txt|uniq  //必须先给文件排序,才能生效
1
11
1213
2
2221dedd
3
3232
32e4
444
ded
dede
root@localhost:~# sort 2.txt|uniq -c  //统计重复次数,
      1 1
      1 11
      1 1213
      2 2
      1 2221dedd
      2 3
      1 3232
      1 32e4
      1 444
      2 ded
      1 dede
  • [ ] tee 和>类似,重定向的同时还在屏幕显示
root@localhost:~# echo "dadsaddsdad" |tee a.txt
dadsaddsdad
root@localhost:~# sort 2.txt|uniq -c |tee a.txt
      1 1
      1 11
      1 1213
      2 2
      1 2221dedd
      2 3
      1 3232
      1 32e4
      1 444
      2 ded
      1 dede
  • [ ] te -a 和>>类似,追加重定向

    root@localhost:~# sort 2.txt|uniq -c |tee -a a.txt
      1 1
      1 11
      1 1213
      2 2
      1 2221dedd
      2 3
      1 3232
      1 32e4
      1 444
      2 ded
      1 dede
    root@localhost:~# cat a.txt
      1 1
      1 11
      1 1213
      2 2
      1 2221dedd
      2 3
      1 3232
      1 32e4
      1 444
      2 ded
      1 dede
      1 1
      1 11
      1 1213
      2 2
      1 2221dedd
      2 3
      1 3232
      1 32e4
      1 444
      2 ded
      1 dede
  • [ ] tr 替换字符,tr ‘a‘ ‘b‘,大小写替换tr ‘[a-z]‘ ‘[A-Z]‘ //[]的意思是任选几个括号内的字符
[root@localhost ~]# echo "xavilinux" |tr ‘[al]‘ ‘[AL]‘
xAviLinux
[root@localhost ~]# echo "xavilinux" |tr ‘a‘ ‘A‘
xAvilinux
  • [ ] split 切割,-b大小(默认单位字节),-l行数

-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 -b 500 passwd
[root@localhost split_dir]# ls
passwd  xaa  xab  xac  xad  xae

查看起实际大小,du -sh查看的是块,du -sb 按照byte查找大小

技术分享图片

如果split不指定目标文件名,则会以xaa、xab……这样的文件名来存取切割后的文件。指定目标文件名,示例如下:

[root@localhost split_dir]# rm -f x* //把前面分割好的文件全部删除
[root@localhost split_dir]# split -b 100 passwd 123
[root@localhost split_dir]# ls
123aa  123ad  123ag  123aj  123am  123ap  123as  123av  passwd
123ab  123ae  123ah  123ak  123an  123aq  123at  123aw
123ac  123af  123ai  123al  123ao  123ar  123au  123ax

-l 表示依据行数来分割文档

[root@localhost split_dir]# rm -f 123*
[root@localhost split_dir]# split -l 10 passwd //10行分割
[root@localhost split_dir]# wc -l *
  46 passwd
  10 xaa
  10 xab
  10 xac
  10 xad
   6 xae
  92 总用量

三 shell的特殊符号

1、特殊符号$

$可以用作变量前面的标识符,还可以和!结合使用。

[root@localhost /]# touch 1.txt
[root@localhost /]# ls 1.txt
1.txt
[root@localhost /]# ls !$
ls 1.txt
1.txt

!$表示上条命令的最后一个变量,本例中上条命令最后是1.txt,那么在当前命令下输入!$则表示1.txt

2、特殊符号;

在一行命令中运行两个或两个以上的命令,需要在命令之间加符号;。

[root@localhost /]# ls 1.txt ; wc -l 2.txt
1.txt
8 2.txt
[root@localhost /]# ls 1.txt;wc -l 2.txt //命令之间不空格也是有效的,
1.txt
8 2.txt
3、特殊符号~

符号~代表用户的家目录,root用户的家目录是/root,普通用户的家目录是/home/username。

4、特殊符号&

把一条命令放到后台执行,则需要加上符号&,它通常用于命令运行时间较长的情况。比如,可以用在sleep后,如下所示:

[root@localhost /]# sleep 30 &
[2] 7047
[root@localhost /]# jobs
[1]+  已停止               wc -l(工作目录:~/split_dir)
[2]-  运行中               sleep 30 &
5、重定向符号>、>>、2>和2>>,错误和正确都输入&>

和>>分别表示取代和追加的意思。当我们运行一个命令报错时,报错信息会输出到当前屏幕。如果想重定向到一个文本,则要用重定向符号2>或者2>>,它们分别表示错误重定向和错误追加重定向。

6、中括号[ ]

中括号内为字符组合,代表字符组合中的任意一个,还可以表示一个范围(1-3,a-z)。

7、特殊符号&& ||

在上面刚刚提到了分号,用于多条命令间的分隔符。另外还有两个可以用于多条命令中间的特殊符号,那就是 “&&” 和 “||” 下面把这几种情况全列出:

command1 ; command2

command1 && command2

command1 || command2

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

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

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

[root@localhost /]# ls 1a.txt; wc -l 2.txt
ls: 无法访问1a.txt: 没有那个文件或目录
[2]-  完成                  sleep 30
8 2.txt
[root@localhost /]# ls 1.txt || wc -l 2.txt
1.txt
[root@localhost /]# ls 1.txt && wc -l 2.txt
1.txt
8 2.txt
[root@localhost /]# ls 1a.txt && wc -l 2.txt
ls: 无法访问1a.txt: 没有那个文件或目录

22.Shell特殊符号和cut,sort,wc,uniq,tee,tr,split命令

标签:就是   slim   blog   分割   题目   shell   du -sh   /etc/   错误   

原文地址:http://blog.51cto.com/12995218/2060847

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