Linux 學習之路(四):管道、重定向、正則
管道及IO重定向
運算器、控制器:CPU
儲存器:RAM
輸入裝置/輸出裝置
程式:指令和資料
控制器:指令
運算器:
儲存器:
地址匯流排:記憶體定址
資料匯流排:傳輸資料
控制匯流排:控制指令
暫存器:CPU暫時儲存器
I/O:硬碟
系統設定
預設輸出裝置:標準輸出,STDOUT,1
預設輸入裝置:標準輸入,STDIN,0
標準錯誤輸出:STDERR,2
標準輸入:鍵盤
標準輸出和錯誤輸出:顯示器
I/O重定向:
改變資料的輸入/輸出來源
- 輸出重定向: > 覆蓋輸出
>> 追加輸出
重定向錯誤輸出:2>
追加方式: 2>>
重定向標準輸出或錯誤輸出至同一個檔案: &>
ls /var > /tmp/var.out
#set -C:禁止對已經存在檔案使用覆蓋重定向
shell內建命令:set -C 開啟功能 +C 關閉功能
#不關閉禁止覆蓋輸出的功能,同時覆蓋輸出
ls /var >| /tmp/var.out
既定向正確輸出,也定向錯誤輸出:
ls /varr > /tmp/var3.out 2> /tmp/err.out
ls /var6 &> /tmp/var5.out
- 輸入重定向:<
#tr本不支援檔案,通過輸入重定向將檔案中小寫變大寫
tr 'a-z' 'A-Z' < /etc/fstab
Here Document:<< 在此處生成文件
[[email protected] ~]# cat << END
> The first line.
> The second line.
> END
The first line.
The second line.
cat << EOF
#使用者從鍵盤輸入資料並儲存在檔案中,用於在指令碼中生成檔案
cat >> /tmp/myfile.txt <<EOF
管道:
前一個命令的輸出,作為後一個命令的輸入
命令1 | 命令2 |命令3 |…
[[email protected] ~]# echo "hello,world." | tr 'a-z' 'A-Z'
HELLO,WORLD.
#取出所有使用者名稱並排序
[[email protected] ~]# cut -d: -f1 /etc/passwd|sort
#既輸出到螢幕,也儲存到檔案
[[email protected] ~]# echo "Hello World." | tee /tmp/hello.out
Hello World.
[[email protected] ~]# cat /tmp/hello.out
Hello World.
#顯示檔案行數,且不能顯示其他資訊
wc -l /etc/passwd 顯示檔案的行數
[[email protected] ~]# wc -l /etc/passwd | cut -d' ' -f1
50
[[email protected] ~]# ls /usr/bin|wc -l
1922
[[email protected] ~]# ls -l /usr/bin |head -1
總用量 242488
練習:
1.統計/usr/bin/目錄下的檔案個數
[[email protected] ~]# ls /usr/bin|wc -l
1922
2.取出當前系統上所有使用者的shell,要求,每種shell只顯示一次,並且按順序進行顯示。
[[email protected] ~]# cut -d: -f7 /etc/passwd | sort -u
/bin/bash
/bin/sync
/sbin/halt
/sbin/nologin
/sbin/shutdown
3.思考:如何顯示/var/log目錄下每個檔案的內容型別?
4.取出/etc/inittab檔案的第6行
[[email protected] ~]# head -6 /etc/inittab | tail -1
#
5.取出/etc/passwd檔案中倒數第9個使用者的使用者名稱和shell,顯示到螢幕上並將其儲存至/tmp/users檔案中
[[email protected] ~]# tail -9 /etc/passwd | head -1 |cut -d: -f1,7 |tee /tmp/users
6.顯示/etc目錄下所有以pa開頭的檔案,並統計其個數
[[email protected] ~]# ls -d /etc/pa* | wc -l
3
7.不使用文字編輯器,將alias cls=clear一行內容新增至當前使用者的.bashrc檔案中。
#printf "alias cls=clear" >> ~/.bashrc
grep以及正則表示式
文字查詢的需要:
grep,egrep,fgrep
grep:根據模式,搜尋文字,並將符合模式的文字行顯示出來。
Pattern:文字字元和正則表示式的元字元組合而成的匹配條件
grep [OPTIONS] PATTERN [FILE…]
-i 忽略大小寫
--color 以顏色顯示
-v反向查詢:顯示沒有被模式匹配到的行
-o只顯示被模式匹配到的字串
#匹配到的字串用顏色顯示出來
[[email protected] ~]# grep --color 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[[email protected] ~]# grep -o 'root' /etc/passwd
root
root
root
root
正則表示式:
元字元:
.:匹配任意單個字元
匹配次數(貪婪模式):
*****:匹配其前面的字元任意次
.*:任意長度的任意字元
?:匹配其前面的字元1次或0次
grep 中有部分匹配就可以
\{m,n\}:匹配其前面的字元至少m次,至多n次
[]:匹配指定範圍內的任意單個字元
[^]:匹配指定範圍外的任意單個字元
字符集:[:digit:],[:lower:],[:upper:],[:punct:],[:space:],[:alpha:],[:alnum:]
位置錨定:
^:錨定行首,此字元後面的任意內容必須出現在行首
$:錨定行尾,此字元前面的任意內容必須出現在行尾
^$:空白行
\<或\b:錨定詞首,其後面的任意字元必須作為單詞首部出現
\>或\b:錨定詞尾,其前面的任意字元必須作為單詞的尾部出現
#表示root出現在詞尾
grep "\broot" test2.txt
分組:
\(\)
\(ab\)* 後向引用
\1:第一個左括號以及與之對應的右括號所包括的所有內容
grep '\(1..e\).*\' test3.txt
test3.txt
1 He love his lover.
2 She like her lover.
3 He like his liker.
4 She love her liker.
[[email protected] ~]# grep '\(l..e\).*\1' test3.txt
He love his lover.
He like his liker.
正則表示式:
Basic REGEXP:基本
Extended REGEXP:擴充套件
grep:使用基本正則表示式定義的模式來過濾文字的命令
-E : 使用擴充套件正則表示式
-A #:顯示模式匹配到的那一行以及後面的#行
-B 前面行
-C 上下若干行
[[email protected] ~]# grep -A 2 '^core id' /proc/cpuinfo
core id : 0
cpu cores : 1
apicid : 0
擴充套件正則表示式:
字元匹配:
.
[]
[^]
次數匹配:
*:
?:其前字元一次或0次
+:匹配其前字元至少一次
{m,n}
位置錨定與正則表示式相同
分組:
():分組
\1,\2,\3,…
或者
|:or
(C|c)at:cat/Cat
grep -E =egrep
找ifconfig命令顯示結果中1-255的數字:
ifconfig|egrep --color '\<([1-9]|[1-9]|[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>'
找出ifconfig命令執行結果中的IP地址:
\.讓元字元表示它本身含義
ifconfig|egrep --color '\<([0-9]|[1-9]|[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>\.)(3)\<([0-9]|[1-9]|[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\>'
IPV4
5類:A B C D E
A:1-127
B:128-191
C:192-223