25.shell特殊符号cut命令 sort wc uniq tee tr split命令

8.10 shell特殊符号cut命令

8.11 sort wc uniq命令

8.12 tee tr split命令

8.13 shell特殊符号下

 

8.10 shell特殊符号cut命令:

~1. *任意个任意字符

通配符

~2. ?任意一个字符

代表任意的一个字符

~3. #注释字符

前面加#,代表不生效。可用在解释说明

~4. \脱义字符 (详细看实例)

取消掉原意,只代表他本身,可用\

~5. |管道符 下面有讲和管道符有关的命令

 

 

 

 

实例

4.

[root@afeilinux-01 ~]# a=1

[root@afeilinux-01 ~]# b=2

[root@afeilinux-01 ~]# c=$a$b 我们想让c=$a$b这串字符本身是不生效的,因为我们用$会调用a的变量

[root@afeilinux-01 ~]# echo $c 所以他会显示,a和b的变量

12

[root@afeilinux-01 ~]# c='$a$b' 我们可以加单引号来代表c=$a$b的本身

[root@afeilinux-01 ~]# echo $c

$a$b

[root@afeilinux-01 ~]# c=\$a\$b 也可以用脱义字符\反推一下,来代表$a或$b这些字符本身

[root@afeilinux-01 ~]# echo $c

$a$b

 

----------------------------------------------------------------------------------------------------------------------------------------------------

 

8.11 sort wc uniq命令:

 

几个和管道有关的命令

~1. cut分割,-d分隔符 -f指定段号 -c指定第几个字符

~2. sort排序,-n以数字排序 -r反序 -t分隔符 -kn1/-kn1,n2

遵循阿斯玛ASCII排序

-n会以数字排序,字母和特殊字符会默认为0.需注意

-nr会以数字反序来排列。跟-n是相反的

-t指定几段去排序,几乎用不到,作为了解

~3. wc -l统计行数 -m统计字符数 -w统计词

-l统计行数,之前用过

-m统计字符数时,也会统计隐藏字符,比如换行符$

-w以空白字符作为分隔符,来统计有多少词 用的不多,作为了解

~4. uniq去重,-c统计行数

一般跟sort同时使用,先排序,后去重。sort 2.txt |uniq

 

 

实例:

1.

[root@afeilinux-01 ~]# cat /etc/passwd | head -2 我们列出passwd的前两行

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

[root@afeilinux-01 ~]# cat /etc/passwd | head -2 |cut -d ":" -f 1 用cut分割,-d表示用什么分割,用双引号引起来。-f表示分隔符:的第一段

root
bin

[root@afeilinux-01 ~]# cat /etc/passwd | head -2 |cut -d ":" -f 1,2 cut一二段的时候用,分割

root:x
bin:x

[root@afeilinux-01 ~]# cat /etc/passwd | head -2 | cut -d ":" -f 1-3 cut一二三段的时候,直接写1-3即可

root:x:0
bin:x:1

[root@afeilinux-01 ~]# cat /etc/passwd |head -2 |cut -c 4 用-c的时候就不要用-d -f了,指定第几个字符。前两行的第四个字符就是 t和:

t
:

[root@afeilinux-01 ~]# sort /etc/passwd 会发现sort后的排序是按照abcdefg来排序的

adm:x:3:4:adm:/var/adm:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
chrony:x:998:996::/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
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

[root@afeilinux-01 ~]# head /etc/passwd >> 1.txt 我们再来追加一些到1.txt

[root@afeilinux-01 ~]# vim !$ 并在里面加点特殊字符

vim 1.txt

[root@afeilinux-01 ~]# sort 1.txt 在sort,他会这样排序

222eqweqw

%231243214214214

[244234

adm:x:3:4:adm:/var/adm:/sbin/nologin

!asrar

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

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

]saffsdfds

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

sync:x:5:0:sync:/sbin:/bin/sync

 

3.

[root@afeilinux-01 ~]# wc -l 22.txt 行数 3行

5 22.txt

[root@afeilinux-01 ~]# wc -m 22.txt 统计字符数,隐藏字符也会统计,比如换行符$

71 22.txt

[root@afeilinux-01 ~]# cat -A 22.txt 用cat -A就可以看到换行符

111111111111111$
22222222222222222$
3333333333333333333333$
444444444444$
$

[root@afeilinux-01 ~]# wc -w 22.txt

4 22.txt

[root@afeilinux-01 ~]# cat !$

cat 22.txt
111111111111111
22222222222222222
3333333333333333333333
444444444444

 

4.

[root@afeilinux-01 ~]# cat 2.txt 先cat一下

33

555

666

555

666111

333

444

333

555

666

[root@afeilinux-01 ~]# uniq 2.txt uniq并没有什么效果,因为没有sort


111111111111111
111111111111111
33333333333333
66666666666666
22222222222222
33333333333333
4444444444444
5555555555555
6666666666666
7777777777777777
8888888888888
99999999999999

[root@afeilinux-01 ~]# sort 2.txt | uniq 我们先sort再uniq,就去重了

111111111111111
22222222222222
33333333333333
4444444444444
5555555555555
6666666666666
66666666666666
7777777777777777
8888888888888
99999999999999

 

 

----------------------------------------------------------------------------------------------------------------------------------------------------

 

8.12 tee tr split命令:

 

~5. tee和>类似,重定向的同时还在屏幕显示

需要加|,|tee这样来用

tee -a追加的效果,并在屏幕上显示。类似于>>

~6. tr替换字符,tr 'a' 'b',大小写替换tr '[a-z]' '[A-Z]'

~7. split 切割,-b大小(默认单位字节),-l行数

将一个大文件切割成多个小文件

 

实例:

 

5.

[root@afeilinux-01 ~]# sort 2.txt |uniq -c >a.txt 用>并不会显示在屏幕上

[root@afeilinux-01 ~]# sort 2.txt |uniq -c |tee a.txt 将>换成|tee,重定向的同时还能打印在屏幕上

      2 111111111111111
      1 22222222222222
      2 33333333333333
      1 4444444444444
      1 5555555555555
      1 6666666666666
      1 66666666666666
      1 7777777777777777
      1 8888888888888
      1 99999999999999

 

6.

[root@afeilinux-01 ~]# echo 'aminglinux' |tr 'a' 'b'

bminglinux

[root@afeilinux-01 ~]# echo 'aminglinux' |tr 'a-z' 'A-Z'

AMINGLINUX

[root@afeilinux-01 ~]# echo 'aminglinux' |tr '[al]' '[AL]' 支持多个,但要加[]

AmingLinux

 

7.

[root@afeilinux-01 ~]# find /etc/ -type f -name '*conf' -exec cat {} >> a.txt \;

[root@afeilinux-01 ~]# du -sh a.txt

224K	a.txt

[root@afeilinux-01 test]# split -b 100k a.txt 指定100k一个分割

[root@afeilinux-01 test]# ls

22.txt  2.txt  a.txt  xaa  xab  xac

a.txt xaa xab xac 不指定名字以x开头

[root@afeilinux-01 test]# split -b 100k a.txt abc. 指定名字为abc.开头

[root@afeilinux-01 test]# ls

22.txt  2.txt  abc.aa  abc.ab  abc.ac  a.txt  xaa  xab  xac

[root@afeilinux-01 test]# split -l 1000 a.txt abc. 指定1000行一个分割

[root@afeilinux-01 test]# ls

abc.aa abc.ab abc.ac abc.ad abc.ae abc.af abc.ag a.txt xaa xab xac

 

----------------------------------------------------------------------------------------------------------------------------------------------------

 

8.13 shell特殊符号下:

 

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

~2. ;多条命令写到一行。用分号分割

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

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

~5. > >> 2> 2>> &>

&>表示正确的和错误的都输入到一个文件里去

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

~7. ||和&&,用于命令之间

||运行在shell当中表示或者的意思

||放在两条命令之间,前面的命令执行不成功会执行后面的命令。前面的执行成功不在执行后面的

 

&&与||相反

&&放在两条命令之间,前面的命令执行成功才会执行后面的命令。前面的命令不成功不会执行后面的命令

 

 

实例:

2.

[root@afeilinux-01 ~]# du -sh 22.txt ; wc -l 2.txt 多条命令之间用;分割

4.0K	22.txt
12 2.txt 显示两条不同的命令

 

7.

[root@afeilinux-01 ~]# ls

1.txt 22.txt 2.txt anaconda-ks.cfg

[root@afeilinux-01 ~]# ls 11.txt || wc -l 2.txt ||前面执行不成功,才执行后面的

ls: 无法访问11.txt: 没有那个文件或目录
12 2.txt

[root@afeilinux-01 ~]# ls 1.txt || wc -l 2.txt ||前面执行成功,不执行后面的

1.txt

[root@afeilinux-01 ~]# ls 1.txt && wc -l 2.txt &&前面的执行成功,才执行后面的

1.txt
12 2.txt

[root@afeilinux-01 ~]# ls 11.txt && wc -l 2.txt &&前面的执行不成功,不执行后面的

ls: 无法访问11.txt: 没有那个文件或目录

 

 

 

扩展实例:

如果一个目录不存在,我才创建

[ -d axin ]

[root@afeilinux-01 ~]# ls

1.txt  22.txt  2.txt  abc.aa  abc.ab  abc.ac  abc.ad  abc.ae  abc.af  abc.ag  a.txt  xaa  xab  xac

[root@afeilinux-01 ~]# [ -d axin ] || mkdir axin 使用||,前面的不存在,才创建

[root@afeilinux-01 ~]# ls

1.txt  22.txt  2.txt  abc.aa  abc.ab  abc.ac  abc.ad  abc.ae  abc.af  abc.ag  a.txt  axin  xaa  xab  xac

如果使用&&呢?

[root@afeilinux-01 ~]# [ -d axin ] && mkdir axin 使用&&,前面的虽然成功了,但执行后面已经存在的目录就会报错

mkdir: 无法创建目录"axin": 文件已存在

 

转载于:https://my.oschina.net/u/3866192/blog/3098629

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值