文本查看
cat
cat [OPTION]... [FILE]... 該命令用于正向查看文本文件,但不可分頁,一次性顯示完成
-
參數說明
參數 | 說明 |
---|---|
-E | 顯示行結束符$ |
-n | 對顯示出的每一行進行編號 |
-A | 顯示所有控制符 |
-b | 非空行編號 |
-s | 壓縮連續的空行成一行 |
-T | 顯示tab為^I |
-
示例
tac
tac [OPTION]... [FILE]... 該命令用于將文件已行為單位的反序輸出,即第一行最后顯示,最后一行先顯示,不可分頁,一次性顯示完成(行內順序不變)
-
示例
-
參考rev
[root@centos7 testdir]# cat f2 abcdef abcdef [1..10] 1 2 3 4 5 6 7 8 9 10 [root@centos7 testdir]# rev f2 fedcba fedcba ]01..1[ 01 9 8 7 6 5 4 3 2 1
more
more [OPTIONS...] FILE... 1)-d: 顯示翻頁及退出提示 2)分頁查看文件,不支持向上翻頁,空格想下翻頁
less
該命令用于一頁一頁地查看文件或STDIN輸出。less 命令是man命令使用的分頁器。
-
翻屏快捷鍵
向后翻一屏 :space 向前翻一屏 :b 向后翻半屏 :d 向前翻半屏 :u 向后翻一行 :enter 向前翻一行 :k / y 退出 :q 跳轉到第?行:# 回到文件首部:1G 回到文件尾部:G
-
查找
/KEYWORD:向后搜索 ?KEYWORD:向前搜索 n :下一個 N :前一個 退出 :q
head
head [OPTION]... [FILE]...
#------------------------------------------------------------------- # 1) -c #: 指定獲取前#字節 #------------------------------------------------------------------- [root@centos7 testdir]# head -c 10 passwd root:x:0:0 #------------------------------------------------------------------- # 2) -n #: 指定獲取前#行 #------------------------------------------------------------------- [root@centos7 testdir]# head -n 2 passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin #------------------------------------------------------------------- # 2) -# :指定行數 #------------------------------------------------------------------- [root@centos7 testdir]# head -2 passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin #------------------------------------------------------------------- # 3) -q :結果不打印文件名(默認) #------------------------------------------------------------------- [root@centos7 testdir]# head -2 -q passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin #------------------------------------------------------------------- # 4) -v :結果打印文件名 #------------------------------------------------------------------- [root@centos7 testdir]# head -2 -v passwd ==> passwd <== root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin
tail
tail [OPTION]... [FILE]...
#------------------------------------------------------------------- # 1) -c #: 指定獲取前#字節 #------------------------------------------------------------------- [root@centos7 testdir]# tail -c 14 passwd god is a girl #------------------------------------------------------------------- # 2) -n #: 指定獲取前#行 #------------------------------------------------------------------- [root@centos7 testdir]# tail -n 1 passwd god is a girl #------------------------------------------------------------------- # 3) -# :指定行數 #------------------------------------------------------------------- [root@centos7 testdir]# tail -1 passwd god is a girl #------------------------------------------------------------------- # 4) -f : 跟蹤顯示文件新追加的內容,常用日志監控 #------------------------------------------------------------------- [root@centos7 testdir]# tail -f passwd liang:x:1000:1000:liang:/home/liang:/bin/bash wangcai:x:1001:1001:wangcai,tiantang,110,120:/home/wangcai:/bin/csh god is a girl #------------------------------------------------------------------- # 5) 監控日志信息,并將其放到后臺,不影響前臺執行命令 #------------------------------------------------------------------- [root@centos7 testdir]# tail -n 0 -f /var/log/messages & ### 監控并扔到后臺 [2] 4497 [root@centos7 testdir]# Aug 5 10:21:29 centos7 root: this is 4test ### 日志變化后,立刻顯示 [root@centos7 testdir]# jobs ### 查看任務??? [1]- Running tail -f /var/log/messages & [2]+ Running tail -n 0 -f /var/log/messages & [root@centos7 testdir]# fg 1 ### ??? tail -f /var/log/messages ^C
文本抽取與合并
cut
cut [OPTION]... [FILE]...
#------------------------------------------------------------------- # 1) -d DELIMITER: 指明分隔符,默認tab # 2) -f FILEDS: # #: 第#個字段 # #,#[,#]:離散的多個字段,例如1,3,6 # #-#:連續的多個字段, 例如1-6 # 混合使用:1-3,7 # 3) -c 按字符切割 # 4) --output-delimiter=STRING指定輸出分隔符 #------------------------------------------------------------------- [root@centos7 testdir]# tail -1 passwd god is a girl [root@centos7 testdir]# tail -1 passwd |cut -d" " -f1-3 --output-delimiter=% god%is%a [root@centos7 testdir]# tail -1 passwd | cut -c10-13 girl
paste
paste [OPTION]... [FILE]...
#------------------------------------------------------------------- # -d 分隔符 : 指定分隔符,默認用TAB # -s : 所有行合成一行顯示 #------------------------------------------------------------------- [root@centos7 testdir]# echo "life is " > f3 [root@centos7 testdir]# echo "good" > f4 [root@centos7 testdir]# paste -d"*" f3 f4 life is *good [root@centos7 testdir]# paste -d"*" -s f3 f4 life is good [root@centos7 testdir]# paste f3 f4 life is good [root@centos7 testdir]# paste -s f3 f4 life is good
文本統計與分析
wc
用于統計輸入的單詞總數、行總數、字節總數和字符總數,可對文件或STDIN中的數據運行。
參數 | 說明 |
---|---|
-l | 只計數行數 |
-w | 只計數單詞總數 |
-c | 只計數字節總數 |
-m | 只計數字符總數 |
$ wc story.txt 39 237 1901 story.txt 行數 單詞數 字符數 文件名
sort
sort [options] file(s) 把整理過的文本顯示在STDOUT,不改變原始文件
參數 | 說明 |
---|---|
-r | 執行反方向(由上至下)整理 |
-n | 執行按數字大小整理 |
-f | 選項忽略(fold)字符串中的字符大小寫 |
-u | 選項(獨特,unique)刪除輸出中的重復行 |
-t c | 選項使用c做為字段界定符 |
-k X | 選項按照使用c字符分隔的X列來整理能夠使用多次 |
#------------------------------------------------------------------- # 將passwd用:分隔,將第三列按照數字進行排序 #------------------------------------------------------------------- [root@centos7 testdir]# cat passwd | sort -n -t: -k3 root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin
uniq
uniq [OPTION]... [FILE]... uniq命令:從輸入中刪除重復的前后相接的行,常和sort 命令一起配合使用:sort userlist.txt | uniq -c
參數 | 說明 |
---|---|
-c | 顯示每行重復出現的次數; |
-d | 僅顯示重復過的行; |
-u | 僅顯示不曾重復的行;連續且完全相同方為重復 |
[root@centos7 testdir]# cat /etc/init.d/functions |tr -cs "[:alpha:]" '\n'|sort|uniq -c|sort -nr 67 pid 55 if 54 file 51 echo [root@centos7 testdir]# df|tr -s " " |sort -nr -t" " -k5|cut -d" " -f5 100% 71% 4% 1% 1% Use% 0% [root@centos7 testdir]# cat passwd |sort -nr -t: -k3|head -1|cut -d: -f1,3,7 nfsnobody:/sbin/nologin
diff & patch
diff用于比較兩個文件的差異,patch則可以用于通過差異和一個文件恢復另一個文件。 這兩個命令與git的命令很類似,可用于版本管理。
[root@centos7 testdir]# echo "god is a girl." > f1 [root@centos7 testdir]# echo "got is A garl." > f2 [root@centos7 testdir]# diff f1 f2 |tee diff ### 比較f1與f2的差異1c1 < god is a girl. --- > got is A garl. [root@centos7 testdir]# diff -u f1 f2 > diff ### 比較f1,f2,并將差異導出為patch文件 [root@centos7 testdir]# cat diff --- f1 2016-08-08 16:22:35.575006634 +0800 ### -代表f1 +++ f2 2016-08-08 16:22:52.175007341 +0800 ### +代表f2 @@ -1 +1 @@ ### 第一行有不同 -god is a girl. ### f1的第一行內容 +got is A garl. ### f2的第一行內容 [root@centos7 testdir]# rm -f f2 ### 刪除f2 [root@centos7 testdir]# cp f1 f1.bak ### 將f1備份 [root@centos7 testdir]# patch -b f1 diff ### 通過f1、diff恢復f2,恢復后名為f1,覆蓋原f1,故要備份 bash: patch: command not found... ### CentOS7可以man出patch命令,但不可執行,CentOS6可以 [root@localhost ~]# patch -b f1 diff ### CentOS6執行patch恢復的結果 patching file f1 [root@localhost ~]# cat f1 got is A garl.
原創文章,作者:taobaibai,如若轉載,請注明出處:http://www.www58058.com/31592