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

10月15日任務linux

8.10 shell特殊符號cut命令正則表達式

8.11 sort_wc_uniq命令shell

8.12 tee_tr_split命令centos

8.13 shell特殊符號下bash

 

8.10 shell特殊符號cut命令ssh

特殊符號post

  • *任意個任意字符排序

  • ? 任意一個字符文檔

  • #  註釋字符it

  • \  脫義字符

  • |  管道符

 

[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.1三、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
相關文章
相關標籤/搜索