分頁查看文件內容
less和more命令介紹 分頁查看文件內容:less和more less:一頁一頁地查看文件或STDIN輸出 # less FILE less的快捷鍵: Ctrl+u:向上翻半屏 Ctrl+d:向下翻半屏 Ctrl+f:向下翻一屏 Ctrl+b:向上翻一屏 ! COMMAND:執行shell命令 /string:搜索string ?string:搜索string n/N:跳到下一個 or 上一個匹配 q:退出 注:less命令是man命令使用的分頁器 more: 分頁查看文件 # more FILE more [OPTIONS...] FILE... -d: 顯示翻頁及退出提示
[root@centos7 ~]# more /etc/rc.d/init.d/functions [root@centos7 ~]# less /etc/rc.d/init.d/functions [root@centos7 ~]# more -d /etc/rc.d/init.d/functions
文件查看
cat命令介紹 文件查看命令:cat, tac,rev cat [OPTION]... [FILE]... -E: 顯示行結束符$ -n: 對顯示出的每一行進行編號 -A:顯示所有控制符 -b:非空行編號 -s:壓縮連續的空行成一行
[root@centos7 tmp]# cat file.txt [root@centos7 tmp]# cat -E file.txt [root@centos7 tmp]# cat -n file.txt [root@centos7 tmp]# cat -A file.txt [root@centos7 tmp]# cat -b file.txt [root@centos7 tmp]# cat -s file.txt
顯示文本前或后行內容
head head [OPTION]... [FILE]... -c #: 指定獲取前#字節 -n #: 指定獲取前#行 -#:指定行數 tail tail [OPTION]... [FILE]... -c #: 指定獲取后#字節 -n #: 指定獲取后#行 -#: -f: 跟蹤顯示文件新追加的內容,常用日志監控
[root@centos7 tmp]# head -5 /etc/passwd 顯示/etc/passwd文件的前5行內容 [root@centos7 tmp]# tail -5 /etc/passwd 顯示/etc/passwd文件的后5行內容
按列抽取文本cut和合并文件paste
cut [OPTION]... [FILE]... -d DELIMITER: 指明分隔符,默認tab -f FILEDS: #: 第#個字段 #,#[,#]:離散的多個字段,例如1,3,6 #-#:連續的多個字段, 例如1-6 混合使用:1-3,7 -c按字符切割 --output-delimiter=STRING指定輸出分隔符 E.G 顯示文件或STDIN數據的指定列 cut-d:-f1/etc/passwd cat /etc/passwd|cut-d:-f7 cut-c 2-5 /usr/share/dict/words paste 合并兩個文件同行號的列到一行 paste [OPTION]... [FILE]... -d 分隔符:指定分隔符,默認用TAB -s : 所有行合成一行顯示 paste f1 f2 paste -s f1 f2
[root@centos7 tmp]# cut -d: -f1,3,7 /etc/passwd | head -5 root:0:/bin/bash bin:1:/sbin/nologin daemon:2:/sbin/nologin adm:3:/sbin/nologin lp:4:/sbin/nologin 以冒號為分隔符,顯示/etc/passwd下第一列、第三列、第七列
文本數據統計:wc
計數單詞總數、行總數、字節總數和字符總數 可以對文件或STDIN中的數據運行 # wc FILE 使用-l來只計數行數 使用-w來只計數單詞總數 使用-c來只計數字節總數 使用-m來只計數字符總數
[root@centos7 tmp]# wc file.txt 2 3 12 file.txt 對應的分別為:行數、單詞數、字節數(包括特殊字符) [root@centos7 tmp]# cat -A file.txt my name is$ $ [root@centos7 tmp]# wc -l file.txt 2 file.txt [root@centos7 tmp]# wc -w file.txt 3 file.txt [root@centos7 tmp]# wc -c file.txt 12 file.txt
文本排序sort
把整理過的文本顯示在STDOUT,不改變原始文件 # sort[options]file(s) 常用選項 -r執行反方向(由上至下)整理 -n執行按數字大小整理 -f選項忽略(fold)字符串中的字符大小寫 -u選項(獨特,unique)刪除輸出中的重復行 -t c選項使用c做為字段界定符 -k X選項按照使用c字符分隔的X列來整理能夠使用多次
uniq
uniq命令:從輸入中刪除重復的前后相接的行 uniq[OPTION]... [FILE]... -c: 顯示每行重復出現的次數; -d: 僅顯示重復過的行; -u: 僅顯示不曾重復的行; 連續且完全相同方為重復 常和sort 命令一起配合使用: sort FILE | uniq-c
比較文件diff
比較兩個文件之間的區別 # difffoo.conf-brokenfoo.conf-works 5c5 <use_widgets=no --- >use_widgets=yes 注明第5行有區別(改變)
復制對文件改變patch
diff命令的輸出被保存在一種叫做“補丁”的文件中 使用-u選項來輸出“統一的(unified)”diff格式文件,最適用于補丁文件。 patch命令復制在其它文件中進行的改變(要謹慎使用!) 適用-b選項來自動備份改變了的文件 # diff -u foo.conf-brokenfoo.conf-works > foo.patch # patch -b foo.conf-brokenfoo.patch 向文件打補丁 patch [OPTIONS] -i /PATH/TO/PATCH_FILE /PATH/TO/OLDFILE patch /PATH/TO/OLDFILE < /PATH/TO/PATCH_FILE
grep
文本處理工具之grep、egrep和fgrep grep:(global search regular expression(RE)and print out the line) 文本搜索工具,根據用戶指定的文本模式對目標文件進行逐行搜索,并顯示匹配的行 格式:grep [options] 'PATTERF' file,默認只支持基本正則表達式, 查看幫助: [root@centos7 tmp]# man 7 regex 要匹配額外功能的字符用-E 選項: --color=auto 指定顏色 -v:反向匹配,顯示不能被模式匹配到的行 -o:僅匹配被模式匹配到的字串,而非整行 -i:不區分大小寫,ignore-case -c: 統計匹配的行數 -n:顯示匹配的行號 -q: 靜默模式,不輸出任何信息 -E:支持擴展 -A #:還顯示模式下面的一行 -B #:還顯示模式上面的一行 -C #:前后各顯示一行 -e:實現多個選項間的邏輯or關系 grep –e ‘cat ’ -e ‘dog’ file -w:整行匹配整個單詞 正則表達式:是一類字符所書寫出的模式(pattern) 元字符:類似統配符,不表示字符本身的意義,用于額外功能性的描述 正則表達式引擎: 采用不同算法,檢查處理正則表達式的軟件模塊 PCRE(Perl Compatible Regular Expressions) 基本正則表達式的元字符: 字符匹配: .:任意單個字符 []:指定范圍內的任意單個字符 [0-9],[[:digit:]] [a-z],[[:lower:]] [A-Z],[[:upper:]] 所有的字母:[[:alpha:]] 字母+數字:[[:alnum:]] 空格:[[:space:]] 標點符號:[[:putct:]] [^]: 指定范圍外的任意單個字符 次數匹配:用來指定匹配其前面的字符的次數 *:任意次 例子:x*y xxy xy y 都能匹配 .*:匹配任意長度的任意字符 \?:前面字符出現0次或者1次的 \+:前面字符至少要出現1次 \{m\}:匹配m次 \{m,n\}:至少m次,至多n次 \{m,\}:至少m次 \{0,n}:至多n次 工作模式為:貪婪模式;盡可能的長的去匹配字符: 位置錨定:用于指定字符出現的位置 ^:錨定行首 ^Char $:錨定行尾 grepchar$ ^$:空白行 ^[[:space:]]*$ :空白行 單詞的位置錨定 \<char :錨定詞首,\bchar char\> : 錨定詞尾,char\b \<hello\>:錨定單詞 分組: \(\) \(ab\)*xy 引用: \1:后向引用,引用前面的第一個左括號以及與之對應的右括號中的模式匹配到的內容, 意思是說前面出現一次,在\1這也要出現一次 E.G:\(a.b\)xy\1: a6bxya6b He like his lover. She love her liker. He love his lover. She like her liker. [root@centos7 ~]# grep "\(l..e\).*\1r" grep.txt egrep: 使用擴展正則表達來構建模式,相當于grep -E 字符匹配: .: 任意單個字符 []: 指定范圍內的任意單個字符 [^]: 指定范圍外的任意單個字符 次數匹配: *:匹配其前面的字符任意次; ?: 匹配其前面的字符0或1次; +: 匹配其前面的字符至少1次 {m}: 匹配其前面的字符m次; {m,n}: 至少m次,至多n次 {m,}: 至少m次; {0,n}:至多n次; 位置錨定: ^: 行首 $: 行尾 \<, \b: 詞首 \>, \b:詞尾 分組: (): 分組 |: 或者 a|b C|cat: C或cat (C|c)at:Cat或cat
最后附錄上一些課堂練習 (自己寫的,非標準答案) 1、找出ifconfig命令結果中本機的所有IPv4地址 2、查出分區空間使用率的最大百分比值 3、查出用戶UID最大值的用戶名、UID及shell類型 4、查出/tmp的權限,以數字方式顯示 5、統計當前連接本機的每個遠程主機IP的連接數,并按從大到小排序 6、顯示/proc/meminfo文件中以大小s開頭的行;(要求:使用兩種方式) 7、顯示/etc/passwd文件中不以/bin/bash結尾的行 8、顯示用戶rpc默認的shell程序 9、找出/etc/passwd中的兩位或三位數 10、顯示/etc/grub2.cfg文件中,至少以一個空白字符開頭的且后面存非空白字符的行 11、找出"netstat -tan"命令的結果中以'LISTEN'后跟0、1或多個空白字符結尾的行 12、添加用戶bash、testbash、basher以及nologin(其shell為/sbin/nologin),而后找出/etc/passwd文件中用戶名同shell名的行 13、顯示當前系統root、mage或wang用戶的UID和默認shell 14、找出/etc/rc.d/init.d/functions文件中行首為某單詞(包括下劃線)后面跟一個小括號的行 15、使用egrep取出/etc/rc.d/init.d/functions中其基名 16、使用egrep取出上面路徑的目錄名 17、統計以root身份登錄的每個遠程主機IP地址的登錄次數 18、利用擴展正則表達式分別表示0-9、10-99、100-199、200-249、250-255 19、顯示ifconfig命令結果中所有IPv4地址
自己做答的:
1、[root@centos7 ~]# ifconfig | tr -cs "[:digit:].[:digit:]" '\n' | sort -u -n -t'.' -k3 2、[root@centos7 ~]# df | tr -s ' ' ':' | cut -d: -f5 | sort -r 3、[root@centos7 ~]# cat /etc/passwd | sort -t':' -k 3 -n -r | head -1 | cut -d: -f1,3,7 4、[root@centos7 ~]# stat /tmp/functions | tr -s ' ' ':' | head -4 | tail -1 | tr -cs [:alnum:] ':' | cut -d: -f2 5、[root@centos7 ~]# netstat -nt | tr -s ' ' | cut -d' ' -f5 | sort -n 6(1)、[root@centos7 ~]# grep -e ^S -e ^s /proc/meminfo 6(2)、[root@centos7 ~]# grep ^[Ss] /proc/meminfo 6(3)、[root@centos7 ~]# grep -i ^s /proc/meminfo 7、[root@centos7 ~]# grep -v "/bin/bash$" /etc/passwd 8、[root@centos7 ~]# grep "^\<rpc\>" /etc/passwd | cut -d: -f1,7 9、[root@centos7 ~]# cat /etc/passwd | grep '\<[1-9][0-9]\{1,2\}\>' 10、[root@centos7 ~]# cat /etc/grub2.cfg | grep '^[[:space:]]\+[^[:space:]]' 11、[root@centos7 ~]# netstat -tan | grep "LISTEN[[:space:]]*$" 12、[root@centos7 ~]# cat /etc/passwd | grep "^\<\(.*\)\>.*/\1$" 13、[root@centos7 tmp]# egrep "^\<(root|mage|wang)\>" /etc/passwd | cut -d: -f1,3,7 14、[root@centos7 tmp]# egrep "^(\<[^[:space:]]*\>|_)\(\)" /etc/rc.d/init.d/functions 15、[root@centos7 tmp]# echo "/etc/rc.d/init.d/functions/" | egrep "[^/]+/?$" 16、[root@centos7 ~]# echo "/etc/sysconfig/network-scripts/" | sed 's@[^/]\+/\?$@@' 17、[root@centos7 tmp]# last | grep "root" | egrep "[[:digit:]]\.{1,3}" | tr -s ' ' | cut -d' ' -f1,3 | sort | uniq -c | sort -nr 18、[root@centos7 ~]# ifconfig |egrep -o "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])" 19、[root@centos7 ~]# ifconfig |egrep -o "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])"
原創文章,作者:sixijie,如若轉載,請注明出處:http://www.www58058.com/29754