格式說明:
操作
概念
命令
說明及舉例
七.cat、cut、less、head、tail、wc、sort、uniq、grep
cat
cat -A a.txt 查看隱藏內容 cat -n a.txt 顯示行號 cat -s a.txt 壓縮空行,把多行空行壓縮為一行 tac a.txt 反著顯示文件內容(反寫命令cat) rev a.txt 文件中每行的內容反向顯示 cat f1 f2 合并f1 f2 內容
more 分頁顯示
more -d a.txt 空格翻頁,回車一行行看
less
可以往回翻頁,n往下翻頁,N往上翻頁, /+內容搜索
head
默認顯示文件前十行
head -n 30 file 顯示文件前30行
tail
默認顯示文件后10行
tail -n30 file 顯示文件后30行 tail -n20 -f file 動態顯示文件后20行 tail -n0-f f1 & 后臺運行,有新變化時候顯示 jobs 查看后臺運行的程序 fg 1 ctrl c結束
cut 從文件中取部分內容,取列
cut -d: -f1,3,5-7 file 分隔符為:取文件中第1,3,5-7列內容 cut -c1-2 file 取文件1-2個字符數 getent passwd|cut -d: -f1-3 --output-delimiter=* 分隔符替代為*
paste
paste -d: f1 f2 把f1 f2 文件內容按行合并,以:分隔
wc 文本數據統計
wc f1 顯示文件f1有多少行、單詞、字符 wc -l f1 只統計行 wc +輸入 ctrl+d結束,統計輸入的數據
sort 文本排序
sort -t: -k 3 -nru /etc/passwd 以:為分隔符對第三列進行倒序數字大小排序并合并重復
echo 最大使用率為:
df|cut -c 44-47|sort -n|tail -2|head -1
uniq 從輸入中刪除重負的前后相接的行
uniq -c f2 顯示每行重復出現的次數 -d 僅顯示重復行 -u 僅顯示不重復的行
diff 比較兩個文件的不同
diff -u f1 f11 >diff.log 比較兩個文件并把結果存到diff.log中 刪除f11后 patch -b f1 diff.log 恢復f11名稱為f1 并把原有f1重命名
looger "this is a test log"
ps axo user.ruser.group,rgroup.cmd 看進程有效用戶,真正發起的用戶,有效組,真正執行的組,執行的命令 netstat -tn 查看鏈接
grep 文本過濾
主要功能:從文本中過濾出特定的行 grep 支持正則表達式 egrep 支持擴展的正則表達式 fgrep 不支持正則表達式(速度快)
grep root /etc/passwd 從passwd文件中搜索帶root的行并打印 grep -n -A3 root /etc/passwd顯示匹配行及其后的3行 grep -n -B3 root /etc/passwd顯示匹配行及之前的3行 grep -n -C3 root /etc/passwd顯示匹配行及其前后的3行 grep -n -C3 -e root -e home /etc/passwd顯示包含root或home的行及其前后的3行 grep "$USER" /etc/passwd -v 顯示與搜索條件不匹配的行 -i 忽略大小寫 -n 顯示匹配的行號 -c 統計匹配的行數 -o 只顯示匹配到的字符串 -q 靜默模式 echo &? 顯示上一個命令是否執行成功,成功顯示0 -e 或者 -e root -e home 包括root或者home -w 單行單詞過濾 -E
正則表達式
元字符分類:字符匹配、匹配次數、位置錨定、分組
字符匹配
. 匹配任意單個字符
grep r..t /etc/passwd
[] 匹配指定范圍內的任意單個字符
grep r[a-Z][a-Z]t /etc/passwd
[^]匹配指定范圍外的任意單個字符
匹配次數(看右邊,左邊有轉譯)
* 匹配前面的字符任意次,包括0次
ro*t rot roooot
\?匹配前面的字符0次或者1次
grep "ro\?t" /etc/passwd
\+ 匹配前面的字符1次以上
\{17\}匹配前面的字符17次
\{1,19\}匹配前面的字符1到19次之內
grep "ro{1,19\}t" /etc/passwd
\{,16\}匹配前面的字符最多16次
\{18,\}匹配前面的字符最少18次
位置錨定 定位出現的位置
^表示行首
grep ^root /etc/passwd 搜出以root開頭的行
$表示行尾
grep root$ /etc/passwd 搜出以root結尾的行
grep ^root.bash$ /etc/passwd 搜出以root開頭以bash結尾的行
grep "^$" /etc/passwd 搜出空行
grep -v "^$" /etc/passwd 搜出非空行
grep -v "^[[:space:]]$" /etc/passwd 搜出非空行,包括空格
\<\ >用于單詞的錨定 分開就是詞首\<和詞尾\>
grep "\<root>" /etc/passwd 搜出包含整個root單詞的行
\b可以錨定詞首或詞尾
分組
\(root\)\+ 表示root單詞重復1次以上
grep "\(r.t).*\(r..t).*\1" f1 這句話代表第一個分組(r.t)的匹配結果中的第一個,到\1的一段內容都符合的行,假如(r.t)的結果是rat,那么\1的結果也是rat,最后的結果就是篩選出包含類似結果為"rat…raat..rat"的行,如果是\1換成\2,則是匹配(r..t)的
總結:
練習:
1.cat /proc/meminfo |grep -i ^s (grep -i ^s /proc/meminfo)
2.cat /etc/passwd|grep -v "/bin/bash"$ (grep -v "/bin/bash"$ /etc/passwd)
3. cat /etc/passwd|grep '^rpc\b'|cut -d: -f7 (grep '^rpc\b'|cut -d: -f7|cut -d: -f7)
4. grep "\<[[:digit:]]{2,3}>" /etc/passwd
5. grep "^[[:space:]]+[^[:space:]]." /testdir/f1
6. netstat -tan|grep "\bLISTEN[[:space:]]$"
7.
egrep 擴展正則表達式
重復多次o,不用加\了
練習:
1.grep -E "^(root|mage|wang)\b" /etc/passwd|cut -d: -f1,3,7
2.grep -E "^([[:alpha:]_])+().*" /etc/rc.d/init.d/functions
3.
/etc/rc.d/init.d/functions
正則表達式表示ip
正則表達式表示手機號
郵箱
sed 文本編輯工具
awk 文本報告生成器
原創文章,作者:自己泡面,如若轉載,請注明出處:http://www.www58058.com/39191