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

10月15日任务

8.10 shell特殊符号cut命令

8.11 sort_wc_uniq命令

8.12 tee_tr_split命令

8.13 shell特殊符号下

 

8.10 shell特殊符号cut命令

特殊符号

  • *任意个任意字符

  • ? 任意一个字符

  • #  注释字符

  • \  脱义字符

  • |  管道符

 

[root@centos6 ~]# a=1
[root@centos6 ~]# b=2
[root@centos6 ~]# c=$a$b
[root@centos6 ~]# echo $c
12
[root@centos6 ~]# c='$a$b'
[root@centos6 ~]# echo $c
$a$b
[root@centos6 ~]# c=\$a\$b
[root@centos6 ~]# echo $c
$a$b
[root@centos6 ~]# cat /etc/passwd |tail -n2 |cut -d ":" -f 1-4
sshd:x:74:74
ntp:x:38:38

##几个和管道符有关的命令

  • cut 分割,-d分隔符 -f指定段号 -c 指定第几个字符
  • sort 排序 ,-n 以数字排序  -r 反序   -t 分隔符   -kn1/-kn1,n2
  • wc -l 统计行数   -m 统计字符  -w  统计词
  • uniq   去重 ,-c  统计行数
  • tee   和>类似   ,    -a  表示追加    重定向的同事还在屏幕显示
  • tr   替换字符 ,tr 'a' 'b',大小写替换tr '[a-z]' '[A-Z]'
  • split 切割 ,-b大小  (默认单位字节), -l行数
[root@centos6 ~]# cat /etc/passwd |head -2
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@centos6 ~]# cat /etc/passwd |head -2  |cut -d ":" -f 1
root
bin
[root@centos6 ~]# cat /etc/passwd |head -2  |cut -d ":" -f 1,2
root:x
bin:x
[root@centos6 ~]# cat /etc/passwd |head -2  |cut -d ":" -f 1-3
root:x:0
bin:x:1
[root@centos6 ~]# cat /etc/passwd |head -2  |cut -c 4
t
:
#sort排序按照阿斯玛排序 
[root@centos6 ~]# sort /etc/passwd
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
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
gopher:x:13:30:gopher:/var/gopher:/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
nobody:x:99:99:Nobody:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin

 

# uniq去重命令使用,只能去掉连续相同的字符  ,常和sort一起使用

[root@centos6 ~]# cat 2.txt
1234
1234
12
ab
ac
ab
[root@centos6 ~]# uniq 2.txt
1234
12
ab
ac
ab
[root@centos6 ~]# sort 2.txt |uniq
12
1234
ab
ac

 

#tee命令输出重定向,不仅重定向,而且还能打印在在屏幕上。sort、uniq这些命令不会修改原始文档,只会把执行的结果显示出来。

[root@centos6 ~]# sort 2.txt  |uniq -c |tee a.txt
      1 12
      2 1234
      2 ab
      1 ac
[root@centos6 ~]# cat a.txt
      1 12
      2 1234
      2 ab
      1 ac
[root@centos6 ~]# sort 2.txt  |uniq -c |tee -a a.txt
      1 12
      2 1234
      2 ab
      1 ac
[root@centos6 ~]# cat a.txt
      1 12
      2 1234
      2 ab
      1 ac
      1 12
      2 1234
      2 ab
      1 ac

#tr命令用法

[root@centos6 ~]# echo "zhangguoxiang" |tr 'z' 'Z'
Zhangguoxiang
[root@centos6 ~]# echo "zhangguoxiang" |tr '[zgx]' '[ZGX]'
ZhanGGuoXianG
[root@centos6 ~]# echo "zhangguoxiang" |tr '[a-z]' '[A-Z]'
ZHANGGUOXIANG

#split命令用法

[root@centos6 ~]# ls
1.txt  2.txt  anaconda-ks.cfg  a.txt  b.txt  install.log  install.log.syslog
[root@centos6 ~]# split -b 50k a.txt
[root@centos6 ~]# ls
1.txt  anaconda-ks.cfg  b.txt        install.log.syslog  xab  xad
2.txt  a.txt            install.log  xaa                 xac
[root@centos6 ~]# split -b 50k a.txt abc.
[root@centos6 ~]# ls
1.txt  abc.aa  abc.ac  anaconda-ks.cfg  b.txt        install.log.syslog  xab  xad
2.txt  abc.ab  abc.ad  a.txt            install.log  xaa                 xac

 

8.13、shell特殊符号

特俗符号

  • $变量前缀,!$组合,正则里面表示行尾

  • ;多条命令写到一行,用分号分割

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

  • &放到命令后面,会把命令丢到后台

  • >  覆盖    >> 追加   2> 错误覆盖   2>>  错误追加   &>不区分,会把正确和错误的同事覆盖到文件

  • []指定字符中的一个,[0-9],[a-z],[A-Z],[abc]

  • ||(表示或,前边命令执行不成功执行后边一条,前边命令正确就爱不在执行后边命令)和&&(表示且,前边一条执行成功才会执行后边命令),用于命令之间

示例:[root@centos6 ~]# ls 1.txt ;wc -l 2.txt
1.txt
6 2.txt
[root@centos6 ~]# ls 1a.txt ||wc -l 2.txt
ls: cannot access 1a.txt: No such file or directory
6 2.txt
[root@centos6 ~]# ls 1.txt ||2.txt
1.txt
[root@centos6 ~]# ls 1a.txt &&wc -l 2.txt
ls: cannot access 1a.txt: No such file or directory
[root@centos6 ~]# ls 1.txt &&wc -l 2.txt
1.txt
6 2.txt
[root@centos6 ~]# ls -d aminglinux ||mkdir aminglinux        #自己瞎写的方法
ls: cannot access aminglinux: No such file or directory
[root@centos6 ~]# ls
1.txt  abc.aa  abc.ac  aminglinux       a.txt  install.log         xaa  xac
2.txt  abc.ab  abc.ad  anaconda-ks.cfg  b.txt  install.log.syslog  xab  xad
[root@centos6 ~]# ls -d aminglinux ||mkdir aminglinux
aminglinux
[root@centos6 ~]# [ -d zgxlinux ] ||mkdir zgxlinux      #老师方法
[root@centos6 ~]# ls
1.txt   abc.ab  aminglinux       b.txt               xaa  xad
2.txt   abc.ac  anaconda-ks.cfg  install.log         xab  zgxlinux
abc.aa  abc.ad  a.txt            install.log.syslog  xac
[root@centos6 ~]# [ -d zgxlinux ] &&mkdir zgxlinux
mkdir: cannot create directory `zgxlinux': File exists

 

转载于:https://my.oschina.net/u/3959708/blog/2245806

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值