1. 程式人生 > >shell特殊字符

shell特殊字符

自定義 ces roo -- kit stack 倒序 查看內容 amp

特殊符號

  • 通配符,任意個字符
    ? 任意個字符
    .#註釋字符
    \ 脫義字符
    技術分享圖片

    | 管道

幾個和管道有關的命令
cut 選取信息,針對行進行分析
主要參數
-b :以字節為單位進行分割。這些字節位置將忽略多字節字符邊界,除非也指定了 -n 標誌。
-c :以字符為單位進行分割。
-d :自定義分隔符,默認為制表符。
-f :與-d一起使用,指定顯示哪個區域。
-n :取消分割多字節字符。僅和 -b 標誌一起使用。如果字符的最後一個字節落在由 -b 標誌的 List 參數指示的<br />範圍之內,該字符將被寫出;否則,該字符將被排除。
技術分享圖片

sort 排序,默認以ASC碼排序

[root@aminglinux01 ~]# sort /etc/passwd

adm:x:3:4:adm:/var/adm:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
bin:x:1:1:bin:/bin:/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
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
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

sort -r 倒序

wc 統計

-l 統計行數 -m 統計字符數 -w 統計詞
[root@aminglinux01 ~]# wc -l 1.txt
29 1.txt
[root@aminglinux01 ~]# wc -m 1.txt
468 1.txt
[root@aminglinux01 ~]# wc -w 1.txt
27 1.txt

uniq 排序 -c 去重
[root@aminglinux01 ~]# sort 2.txt
1
1
1
123
123
2
2
2
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
21
3
abc
abc 1111,222
[root@aminglinux01 ~]# sort 2.txt | uniq
1
123
2
21
3
abc
abc 1111,222

[root@aminglinux01 ~]# sort 2.txt | uniq -c
3 1
2 123
3 2
21 21
1 3
1 abc
1 abc 1111,222

tee和 >輸出重定向類似

[root@aminglinux01 ~]# >a.txt #清空a.txt內容
[root@aminglinux01 ~]# cat a.txt #查看
[root@aminglinux01 ~]# sort 2.txt | uniq -c | tee a.txt #重定向並顯示
3 1
2 123
3 2
21 21
1 3
1 abc
1 abc 1111,222
[root@aminglinux01 ~]# cat a.txt
3 1
2 123
3 2
21 21
1 3
1 abc
1 abc 1111,222

[root@aminglinux01 ~]# sort 2.txt | uniq -c | tee -a a.txt #追加
3 1
2 123
3 2
21 21
1 3
1 abc
1 abc 1111,222
[root@aminglinux01 ~]# cat a.txt #查看內容
2 123
3 2
21 21
1 3
1 abc
1 abc 1111,222
3 1
2 123
3 2
21 21
1 3
1 abc
1 abc 1111,222

tr 替換字符

[root@aminglinux01 ~]# echo "aminglinux" |tr ‘[al]‘ ‘[AL]‘
AmingLinux
[root@aminglinux01 ~]# echo "aminglinux" |tr ‘a‘ ‘A‘
Aminglinux

split 切割

[root@aminglinux01 test]# split -b 100k a.txt
[root@aminglinux01 test]# ls
a.txt xaa xab
[root@aminglinux01 test]# du -sh
128K a.txt
100K xaa
28K xab
[root@aminglinux01 test]# split -b 100k a.txt abc. #指定大小
[root@aminglinux01 test]# ls
abc.aa abc.ab a.txt
[root@aminglinux01 test]# split -l 1000 a.txt #指定行
[root@aminglinux01 test]# ls -l
總用量 260
-rw-r--r--. 1 root root 129633 1月 13 07:31 a.txt
-rw-r--r--. 1 root root 41537 1月 13 07:32 xaa
-rw-r--r--. 1 root root 35466 1月 13 07:32 xab
-rw-r--r--. 1 root root 36841 1月 13 07:32 xac
-rw-r--r--. 1 root root 15789 1月 13 07:32 xad
[root@aminglinux01 test]# wc -l

3549 a.txt
1000 xaa
1000 xab
1000 xac
549 xad
7098 總用量

特殊符號

$變量前綴
!$組合,正則裏面表示行尾
;多行命令寫到一行,用;分割
~用戶家目錄,正則表達式表示匹配符
&放在命令後,會把命令掛起在後臺

>> 2> 2>> &>
[] 指定字符的一個,[0-9][a-z][A-Z]

||和&& 用於命令中間的邏輯關系

[root@aminglinux01 ~]# ls 1.txt ;wc -l 2.txt
1.txt
32 2.txt
[root@aminglinux01 ~]# ls 1.txt || wc -l 2.txt
1.txt
當前面命令成功,不執行後面的命令
[root@aminglinux01 ~]# ls 1.txt && wc -l 2.txt
1.txt
32 2.txt
[root@aminglinux01 ~]# ls 1a.txt && wc -l 2.txt
ls: 無法訪問1a.txt: 沒有那個文件或目錄
當前面的命令不成功,不執行後面的命令

[root@aminglinux01 ~]# [ -d aminglinux ] || mkdir aminglinux
如果aminglinux 目錄不存在,就創建aminglinux目錄
[-d n]固定格式
[root@aminglinux01 ~]# ls
1.txt 2.txt 3.txt aaaa.txt aminglinux anaconda-ks.cfg a.txt; :q test

[root@aminglinux01 ~]# [ -d aminglinux ] && mkdir aminglinux
mkdir: 無法創建目錄"aminglinux": 文件已存在
[root@aminglinux01 ~]# [ -d aminglinux ] || mkdir aminglinux

shell特殊字符