centos文本查看及处理相关的常用命令

1.文本查看cat less more:

(1)使用cat命令

[root@62q ~]# cat /etc/passwd
[root@62q ~]# cat -n test # -n显示行号
     1    1855
     2    1855
     3    794      

(2)也可以使用less命令可以像man命令一样上下翻屏查看:

[root@62q ~]# less /etc/passwd

(3)查看前n行:head

[root@62q ~]# head test #不加选项默认显示前10行
[root@62q ~]# head -n5 test # -n5表示查看前5行,也可以直接省略n,直接以-5的数字表示
1855
1855
794
3355
97

(4)查看后n行:tail

[root@62q ~]# tail test
[root@62q ~]# tail -n5 test
56
3657
466
466
56
[root@62q ~]# tail -n5 -f test # -f 查看文件尾部,不退出,等待显示后续追加至此文件的新内容

2.文本处理

(1)自定义查看文本:cut

[root@62q ~]# cut -d: -f1-3 /etc/passwd # -d表示分隔符,-f表示行号
root:x:0
bin:x:1
daemon:x:2
adm:x:3
lp:x:4
sync:x:5
shutdown:x:6

(2)文本排序:sort

[root@62q ~]# sort test #按照ascii顺序排序
1855
1855
1855
22356
3355
3657
[root@62q ~]# sort -n test # -n按照数值排序
56
56
97
466
[root@62q ~]# sort -r test # -r降序排序
97
794
56
56
466

(3)删除相邻的重复行:uniq

[root@62q ~]# uniq test #删除重复行(注意,只会删除相邻的重复行)
1855
794
3355
97
1855
3886
22356
56
3657
466
56
[root@62q ~]# uniq -c test # -c显示重复的次数
      2 1855
      1 794
      1 3355
      1 97
      1 1855
      1 3886
      1 22356
      1 56
      1 3657
      2 466
      1 56
[root@62q ~]# uniq -d test -d只显示重复行
1855
466

(4)文本统计: wc

[root@62q ~]# wc test
13 13 57 test #分别为行数 单词数 字节
[root@62q ~]# wc -l test # -l行数
13 test
[root@62q ~]# wc -w test #-w 单词数
13 test
[root@62q ~]# wc -c test # -c字节数
57 test

(5)字符处理命令,用于转换或删除字符:tr

[root@62q ~]# tr "a-z" "A-Z" < /etc/passwd #将passwd文本中的a-z字母转换成A-Z
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
[root@62q ~]# tr -d "a-z"  < /etc/passwd ##将passwd文本中的a-z全部删除
::0:0::/://
::1:1::/://
::2:2::/://
::3:4:://://
::4:7::///://
::5:0::/://
本人熟悉 Windows、Linux、OS X、Android、iOS、WP8 等系统的开关机,精通 CSS、JavaScript、PHP、ASP、C、C++、C#、Java、Ruby、Perl、Lisp、Python、Objective-C、ActionScript、Pascal 的hello world编写。
原文地址:https://www.cnblogs.com/pastman/p/4713821.html