查看文本:
cat、tac、more、less、head、tail
cat:連接並顯示
cat 可以查看多個文件。cat 選項 參數1 參數2 3 .。。。
-n 顯示是顯示編號
-E 顯示結束符
linux中文本文件的行結束符是$ windows 是回車和$
eg1:cat -n
[root@RHEL5 ~]# cat -n sort.test
1 111
2 456
3 67
4 111
eg2:cat -E
[root@RHEL5 ~]# cat -E sort.test
111$
456$
67$
111$
tac和cat的命令使用方法一致,但和cat命令輸出顯示的順序相反。
Ctrl+c 取消命令執行
shift + PageUP PageDown //在終端上操作更多屏
分屏顯示:
more、less
more: 向後翻 翻屏到最後自動退出
支持空格、b、回車,但不支持k鍵
less: 和man命令的翻屏功能一致
翻屏:
向後翻一屏:SPACE
向前翻一屏:b
向後翻一行:ENTER
向前翻一行:k
查找:
/KEYWORD: 向後
n: 下一個
N:前一個
?KEYWORD:向前
n: 下一個
N:前一個
q: 退出
head:查看前n行
tail:查看後n行
-n
tail -f: 查看文件尾部,不退出,等待顯示後續追加至此文件的新內容;
tail -f /tmp/inittab 可以在另一個終端寫入數據,tail -f可以
顯示出已保存的數據
文本處理:
cut、join、sed、awk
cut:
-d: 指定字段分隔符,默認是空格
-f: 指定要顯示的字段
-f 1,3
-f 1-3
eg1:
[root@RHEL5 ~]# cut -d: -f1 /etc/passwd
root
bin
文本排序:sort
默認是升序,且默認是按ASCII表中的順序進行排序
-n:數值排序
-r: 降序
-t: 字段分隔符
-k: 以哪個字段爲關鍵字進行排序 sort -t: -k3 /etc/passwd
按照:爲分隔符,按第三段爲關鍵字
-u: 排序後相同的行只顯示一次
-f: 排序時忽略字符大小寫
eg1:sort
[root@RHEL5 ~]# sort sort.test
111
111
231
231
456
67
eg2:sort -r
[root@RHEL5 ~]# sort -r sort.test
67
456
231
231
111
111
eg3:sort -n
[root@RHEL5 ~]# sort -n sort.test
67
111
111
231
231
456
eg4:sort -t: -k3 -n
[root@RHEL5 ~]# sort -t: -k3 -n /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
news:x:9:13:news:/etc/news:
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
eg5:sort -u
uniq: 略過或顯示重複且相臨的行
-c: 顯示文件中行重複的次數
-d: 只顯示重複的行
-D:顯示所有重複的行
eg1:uniq -c
[root@RHEL5 ~]# uniq -c sort.test
1 111
1 456
1 67
1 111
2 231
eg2:uniq -d
[root@RHEL5 ~]# uniq -d sort.test
231
eg3:uniq -D
[root@RHEL5 ~]# uniq -D sort.test
231
231
文本統計:wc (word count)
-l 行
-w 單詞
-c 字節數
-L 最長一行包含多少字節
eg1:
[root@RHEL5 ~]# wc /etc/fstab
7 42 532 /etc/fstab
行 單詞 字節
eg2:wc -l
[root@RHEL5 ~]# wc -l /etc/fstab
7 /etc/fstab
eg3:wc -w
[root@RHEL5 ~]# wc -w /etc/fstab
42 /etc/fstab
eg4:wc -c
[root@RHEL5 ~]# wc -c /etc/fstab
532 /etc/fstab
eg5:wc -L
[root@RHEL5 ~]# wc -L /etc/fstab
75 /etc/fstab
字符處理命令:tr —— 轉換或刪除字符
tr [OPTION]... SET1 [SET2] 不能直接跟文件可以使用管道符
-d: 刪除出現在字符集中的所有字符
eg1:tr -d
[root@RHEL5 ~]# tr -d 'ab'
accout
ccout
able
le
eg2: