65.shell特殊符号与和cut,sort,wc,uniq,tee,tr,split命令
liunx的特殊符號(hào)
- 代表字母或者數(shù)字 多個(gè)
? 任意一個(gè)字符
"#" 注釋
\ 脫義字符
| 管道符
1.* 代表任意個(gè)任意字符或者數(shù)字
[root@localhost /]# ls *.txt
1.txt
[root@localhost /]#
2.?任意一個(gè)字符
[root@localhost /]# ls ?.txt
1.txt
[root@localhost /]#
3.注釋
[root@localhost /]# #11111
[root@localhost /]#
PS:意思就代表著說明
4、\脫義
[root@localhost /]# ls .txt
1.txt
[root@localhost /]# ls *.txt
ls: 無法訪問.txt: 沒有那個(gè)文件或目錄
[root@localhost /]#
5.管道符
[root@localhost /]# cat /etc/passwd|grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@localhost /]#
cut命令
命令類型
文件管理
命令描述
用來截取某一個(gè)字段來顯示出來
命令語法
cut 【參數(shù)】
參數(shù)
-d, --delimiter=分界符 使用指定分界符代替制表符作為區(qū)域分界
-c, --characters=列表 只選中指定的這些字符
-f :指定某一段
案例:查看/etc/passwd文件 以:為分割符 顯示 第一段
[root@localhost /]# cat /etc/passwd|head |cut -d ":" -f 1
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
[root@localhost /]#
sort命令
命令描述
此命令就是用來針對(duì)文檔進(jìn)行排序的
命令語法
sort [參數(shù)] [文件]
參數(shù)
-t 分割符:和cut -d 一樣
-n :使用純數(shù)字排序
-r :方向排序
-u :去重復(fù)
-k :-kn1,n2 :由n1區(qū)間排序到n2區(qū)間,可以只寫-kn1,即對(duì)n1字段排序
案例一:不加任何參數(shù),會(huì)用ASCII碼值進(jìn)行比較,最后將他們按升序輸出。
[root@localhost /]#
[root@localhost /]# head /etc/passwd|sort
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
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
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sync:x:5:0:sync:/sbin:/bin/sync
[root@localhost /]#
WC命令
命令描述
用于統(tǒng)計(jì)
命令語法
wc [參數(shù)]
參數(shù)
-l:統(tǒng)計(jì)行數(shù)
-m:統(tǒng)計(jì)字符數(shù)
-w:統(tǒng)計(jì)詞數(shù)
案例
[root@localhost /]# cat /etc/passwd |wc -l -m -w
19 29 883
[root@localhost /]# cat /etc/passwd |wc
19 29 883
[root@localhost /]#
PS:其實(shí)不需要加參數(shù)的
unip命令
命令描述
用來刪除重復(fù)的行
命令語法
xxxxx|unip
參數(shù)
-c 統(tǒng)計(jì)重復(fù)的行數(shù)
案例
[root@localhost ~]# uniq testb.txt
111
222
111
333
[root@localhost ~]# sort testb.txt |uniq
111
222
333
[root@localhost ~]# sort testb.txt |uniq -c
2 111
1 222
1 333
tee命令
tee后面跟文件名,其作用類似于重定向>,比重定向多一個(gè)功能,把文件寫入后面所跟的文件時(shí)并顯示在屏幕上,通常用于管道符 |后。
案例
#echo “aaaaaaaaaaaaaaa” |tee testb.txt
aaaaaaaaaaaaaaa
#cat testb.txt
aaaaaaaaaaaaaaa
命令tr
tr用于替換字符,常用來處理文檔中出現(xiàn)的特殊符號(hào),如DOS文檔中出現(xiàn)的符號(hào) ^M 。
選項(xiàng):
參數(shù)
-d 表示刪除某個(gè)字符,后面跟要?jiǎng)h除的字符。
-s 表示刪除重復(fù)的字符。
把小寫字母變成大寫字母,如tr’[a-z]’’[A-Z]’:
案例
#head –n2 /etc/passwd |tr’[a-z]’’[A-Z]’
ROOT:X:O:O:ROOT:/ROOT:/BIN/BASH
BIN:X:11:BIN:/BIN:/SBIN/NOLOGIN
還可以替換一個(gè)字符:
#grep ‘root’ /etc/passwd |tr ‘r’ ‘R’
Root:x:o:o:Root:/Root:/bin/bash
opeRatoR:x:11:0:opeRatoR:/Root:/sbin/nologin
命令split
命令描述
split用于切割文檔,常用的選項(xiàng)為-b和-l。
參數(shù)
-b 表示依據(jù)大小來分割文檔,單位為byte
-l 表示依據(jù)行數(shù)來分割文檔
案例
#split –b 500 passwd
#ls
passwd xaa xab xac xad xae
shell一些其他特殊符號(hào)
1、特殊符號(hào)$
$可以用作變量前面的標(biāo)識(shí)符,還可以和!結(jié)合使用。
#cd ..
#ls testb.txt
testb.txt
#ls !$
ls testb.txt
testb.txt
!$表示上條命令的最后一個(gè)變量,本例中上條命令最后是testb.txt,那么在當(dāng)前命令下輸入!$則表示testb.txt。
2、特殊符號(hào);
在一行命令中運(yùn)行兩個(gè)或兩個(gè)以上的命令,需要在命令之間加符號(hào);。
#mkdir testb.txt ; touch test1.txt ; touch test2.txt ; ls –d test* 創(chuàng)建目錄、創(chuàng)建文件、列出目錄
3、特殊符號(hào)~
符號(hào)~代表用戶的家目錄,root用戶的家目錄是/root,普通用戶的家目錄是/home/username。
4、特殊符號(hào)&
把一條命令放到后臺(tái)執(zhí)行,則需要加上符號(hào)&,它通常用于命令運(yùn)行時(shí)間較長的情況。比如,可以用在sleep后,如下所示:
#sleep 30 &
[1]3008
#jobs
[1]+ 運(yùn)行中 sleep 30 &
5、重定向符號(hào)>、>>、2>和2>>
和>>分別表示取代和追加的意思。當(dāng)我們運(yùn)行一個(gè)命令報(bào)錯(cuò)時(shí),報(bào)錯(cuò)信息會(huì)輸出到當(dāng)前屏幕。如果想重定向到一個(gè)文本,則要用重定向符號(hào)2>或者2>>,它們分別表示錯(cuò)誤重定向和錯(cuò)誤追加重定向。
#ls aaaa
ls:無法訪問aaaa:沒有那個(gè)文件或目錄
#ls aaaa 2> /tmp/error
#cat /tmp/error
ls:無法訪問aaaa:沒有那個(gè)文件或目錄
#ls aaaa 2>> /tmp/error
#cat /tmp/error
ls:無法訪問aaaa:沒有那個(gè)文件或目錄
ls:無法訪問aaaa:沒有那個(gè)文件或目錄
6、中括號(hào)[ ]
中括號(hào)內(nèi)為字符組合,代表字符組合中的任意一個(gè),還可以表示一個(gè)范圍(1-3,a-z)。
#cd /tmp/10
#ls –d test*
test1.txt test2.txt testb.txt testdir
#ls –d test[1-3].txt
test1.txt test2.txt
#ls –d test[12b].txt
test1.txt test2.txt testb.txt
#ls –d test[1-9].txt
test1.txt test2.txt
#ls –d test[1-9a-z].txt
test1.txt test2.txt testb.txt
7、特殊符號(hào)&& ||
在上面剛剛提到了分號(hào),用于多條命令間的分隔符。另外還有兩個(gè)可以用于多條命令中間的特殊符號(hào),那就是 “&&” 和 “||” 下面把這幾種情況全列出:
command1 ; command2
command1 && command2
command1 || command2
使用 ”;” 時(shí),不管command1是否執(zhí)行成功都會(huì)執(zhí)行command2;
使用 “&&” 時(shí),只有command1執(zhí)行成功后,command2才會(huì)執(zhí)行,否則command2不執(zhí)行;
使用 “||” 時(shí),command1執(zhí)行成功后command2 不執(zhí)行,否則去執(zhí)行command2,總之command1和command2總有一條命令會(huì)執(zhí)行。
轉(zhuǎn)載于:https://blog.51cto.com/sdwaqw/2060908
總結(jié)
以上是生活随笔為你收集整理的65.shell特殊符号与和cut,sort,wc,uniq,tee,tr,split命令的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 设置更改root密码 ,连接mysql,
- 下一篇: spring boot logback配