1. 程式人生 > >2018-1-12 Linux學習筆記

2018-1-12 Linux學習筆記

str and passwd 追加 -h 一行 naconda 命令 使用

8.10 shell特殊符號cut命令

8.10.1 特殊符號
符號 * : 任意個任意字符
符號 ? : 任意一個字符
符號 # : 註釋字符,即#後面的內容linux忽略掉
符號 \ : 轉義字符,將後面的特殊符號(例如"")還原為普通字符
符號 | : 管道符,將符號前面命令的結果丟給符號後面的命令後面的命令,後面的命令通常是對文檔操作的命令 ,例如cat,less,head,tail,grep.

8.10.2
cut命令,用來截取某一個字段
語法: cut -d "分隔字符" [-cf] n 這裏的n是數字
-d : 指定分隔字符
-f : 指定段號(段與段間以分隔符隔開)

-c : 指定第幾個字符
<------------------------------------------------------------->
[root@kh-01 ~]# cat /etc/passwd | head -n2
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@kh-01 ~]# cat /etc/passwd | head -n2 | cut -d ":" -f1
root
bin
[root@kh-01 ~]# cat /etc/passwd | head -n2 | cut -d ":" -f3-5
0:0:root
1:1:bin
[root@kh-01 ~]# cat /etc/passwd | head -n2 | cut -c1
r
b
[root@kh-01 ~]# cat /etc/passwd | head -n2 | cut -c1-3
roo
bin
<------------------------------------------------------------->
註意:-c與-f不能同時使用,否則會報錯,如下圖:
<---------------------------------------------------------------->
[root@kh-01 ~]# cat /etc/passwd | head -n2 | cut -c1-3 -d ":" -f1
cut: 只能指定列表中的一種類型
Try ‘cut --help‘ for more information.
[root@kh-01 ~]#
<---------------------------------------------------------------->

8.11 sort_wc_uniq命令

8.11.1
sort命令,用於排序.
語法: sort [-t 分隔符] [-kn1,n2] [-nru] 這裏的n1 < n2
-t : 後跟分隔符,作用跟cut的-d一個意思
-n : 使用純數字排序(註意:像字母,特殊符號之類非數字的,系統會認為它是0)
-r : 反向排序
-u : 去重復
-kn1,n2 : 由n1區間排序到n2區間,可以只寫-kn1,即對n1字段排序
如果sort不加任何選項,則從首字符向後,依次按ASCII碼值進行比較,最後將他們按升序輸出。
8.11.2
wc命令,用於統計文檔的行數、詞數、字符數.
-l : 統計行數
-w : 統計詞數
-m : 統計字符數
wc 不跟任何選項,直接跟文檔,則會把行數、詞數、字符數依次輸出。
<----------------------------------->
[root@kh-01 ~]# wc -l /etc/passwd
25 /etc/passwd
[root@kh-01 ~]# wc -w /etc/passwd
45 /etc/passwd
[root@kh-01 ~]# wc -m /etc/passwd
1169 /etc/passwd
[root@kh-01 ~]# wc /etc/passwd
25 45 1169 /etc/passwd
[root@kh-01 ~]#

註意-m統計字符數,它是會把換行符也算上的!
[root@kh-01 ~]# cat 2.txt
1234
[root@kh-01 ~]# wc -m 2.txt
5 2.txt
[root@kh-01 ~]# cat -A 2.txt
1234$
[root@kh-01 ~]#
<----------------------------------->
8.11.3
uniq命令,用於去除重復的行.
-c : 統計重復的行數,並把行數寫在前面.
註意:使用uniq的前提是需要先給文件排序,否則不管用.
<------------------------------------------->
[root@kh-01 ~]# uniq 2.txt
111
333
222
111
555
[root@kh-01 ~]# sort -n 2.txt | uniq
111
222
333
555
[root@kh-01 ~]# sort -n 2.txt | uniq -c
2 111
1 222
1 333
1 555
[root@kh-01 ~]#
<------------------------------------------->
!!註意,使用sort,uniq命令並不會更改原文件的內容,它們只是對原文件內容做了一些加工然後輸出到屏幕上而已,原文件內容並沒有改變.

8.12 tee_tr_split命令

8.12.1
tee命令
後跟文件名,類似重定向>,但重定向同時還顯示在屏幕上,tee常用於管道符"|"後.
<---------------------------------------------------------->
[root@kh-01 ~]# sort -n 2.txt | uniq -c | tee 1.txt
2 111
1 222
1 333
1 555
[root@kh-01 ~]# cat 1.txt
2 111
1 222
1 333
1 555
tee -a 類似追加重定向>>,如下例:
[root@kh-01 ~]# cat 1.txt
2 111
1 222
1 333
1 555
[root@kh-01 ~]# sort -n 2.txt | uniq -c | tee -a 1.txt
2 111
1 222
1 333
1 555
[root@kh-01 ~]# cat 1.txt
2 111
1 222
1 333
1 555
2 111
1 222
1 333
1 555
[root@kh-01 ~]#
<---------------------------------------------------------->
8.12.2
tr命令
用於替換字符,常用來處理文檔中出現的特殊符號.
最常用的就是把小寫變大寫: tr ‘[a-z]‘ ‘[A-Z]‘
<----------------------------------------------------------------->
[root@kh-01 ~]# echo "helloworld" | tr ‘h‘ ‘H‘
Helloworld
[root@kh-01 ~]# echo "helloworld" | tr ‘[hw]‘ ‘[HW]‘
HelloWorld
[root@kh-01 ~]# echo "helloworldw" | tr ‘[hw]‘ ‘[HW]‘
HelloWorldW
[root@kh-01 ~]# echo "helloworldw" | tr ‘[a-z]‘ ‘[A-Z]‘
HELLOWORLDW
<----------------------------------------------------------------->
不足之處:
tr命令替換、刪除以及去重復都是針對一個字符來講的,不能針對一個字符串,有一定局限性.

8.12.3
split命令
用於切割文檔,常用選項:
-b : 依據大小來分割文檔,默認單位為byte
-l : 依據行數來分割文檔
<----------------------------------------------------------------------------------->
[root@kh-01 test2]# ls
1.txt
[root@kh-01 test2]# du -sh 1.txt
256K 1.txt
[root@kh-01 test2]# split -b 10000 1.txt
[root@kh-01 test2]# ls
1.txt xab xad xaf xah xaj xal xan xap xar xat xav xax xaz
xaa xac xae xag xai xak xam xao xaq xas xau xaw xay
[root@kh-01 test2]# rm -f x
[root@kh-01 test2]# ls
1.txt
[root@kh-01 test2]# split -l 1000 1.txt
[root@kh-01 test2]# ls
1.txt xaa xab xac xad xae xaf xag
[root@kh-01 test2]# du -sh

256K 1.txt
44K xaa
44K xab
40K xac
40K xad
36K xae
40K xaf
20K xag
[root@kh-01 test2]#
<----------------------------------------------------------------------------------->

8.13 shell特殊符號下

  • 符號 $
    變量前綴,!$組合,表示上條命中中最後一個變量,假如上邊命令最後是test.txt那麽****在當前命令下輸入!$則代表test.txt.在正則裏面則表示行尾.
  • 符號 ;
    多條命令寫到一行時,用分號來分隔.
  • 符號 ~
    用戶的家目錄,如果是root則是/root,普通用戶則是/home/username
  • 符號 &
    放到命令後面,會把命令丟到後臺執行.
  • 符號 >
    重定向
  • 符號 >>
    追加重定向
  • 符號 2>
    錯誤重定向
  • 符號 2>>
    錯誤追加重定向
  • 符號 &>
    不論命令執行結果如何(正確/錯誤)都重定向
  • 符號 []
    指定字符串中的一個,[0-9],[a-zA-Z],[abc]
  • 符號 &&
    用於命令之間,比如command1 && command2,只有command1執行成功後,command2才會執行,否則command2不執行.
    符號 ||
    用於命令之間,比如command1 || command2,command1執行成功後command2不執行,否則去執行command2,總之command1和command2總有一條命令會執行.

關於&>的演示示例:
<----------------------------------------------------------------->
[root@kh-01 ~]# ls
2.txt 3.txt anaconda-ks.cfg err.txt result.txt test test2
[root@kh-01 ~]# cat result.txt
[root@kh-01 ~]# cat 2.txt
111
333
222
111
555
[root@kh-01 ~]# cat 2.txt &> result.txt
[root@kh-01 ~]# cat result.txt
111
333
222
111
555
[root@kh-01 ~]# >result.txt  #清空result.txt
[root@kh-01 ~]# cat result.txt
[root@kh-01 ~]# ls 1.txt &> result.txt
[root@kh-01 ~]# cat result.txt
ls: 無法訪問1.txt: 沒有那個文件或目錄
[root@kh-01 ~]#
<----------------------------------------------------------------->

2018-1-12 Linux學習筆記