文本处理工具

文本处理工具

文件查看命令:cat  cat [OPTION]… [FILE]… 

-E: 显示行结束符$ 

-n: 对显示出的每一行进行编号 

-A:显示所有控制符 

-b:非空行编号 

-s:压缩连续的空行成一行

[root@centous1 soft]# echo "1 2 3 4 5 6" >> f1
[root@centous1 soft]# cat f1
1 2 3 4 5 6

more: 分页查看文件 more [OPTIONS…] FILE… 

-d: 显示翻页及退出提示 

less:一页一页地查看文件或STDIN输出 查看时有用的命令包括: /文本 搜索 文本 

n/N 跳到下一个 或 上一个匹配 

less 命令是man命令使用的分页器

注;平时的用法就是,cat FILE | more/less

head head [OPTION]… [FILE]… 

-c #: 指定获取前#字节 

-n #: 指定获取前#行 

-#: 指定行数 

tail tail [OPTION]… [FILE]… 

-c #: 指定获取后#字节 

-n #: 指定获取后#行 

-#:指定行数

-f: 跟踪显示文件新追加的内容,常用日志监控

[root@centous1 soft]# cat f1
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
[root@centous1 soft]# cat f1 | head -n 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
[root@centous1 soft]# cat f1 | tail -n 3
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
[root@centous1 soft]# cat f1 | head -n 4 | tail -n 1
1 2 3 4 5 6 7

cut [OPTION]… [FILE]… 

-d DELIMITER: 指明分隔符,默认tab -f FILEDS: 

#: 第#个字段 #,#[,#]:离散的多个字段,例如1,3,6 #-#:连续的多个字段, 例如1-6 混合使用:1-3,7 

-c 按字符切割 –output-delimiter=STRING指定输出分隔符

[root@centous1 soft]# cat f1 | cut -d " " -f 1
1
1
1
1
1
1
[root@centous1 soft]# cat f1 | cut -d " " -f 1,4,7
1 4
1 4
1 4
1 4 7
1 4 7
1 4 7

注;这里的意思是,cut -d 以空格为分隔符进行切割,-f则是显示第几行,以逗号隔开

wc 计数单词总数、行总数、字节总数和字符总数 可以对文件或STDIN中的数据运行 

使用 -l 来只计数行数 

使用 -w 来只计数单词总数 

使用 -c 来只计数字节总数 

使用 -m 来只计数字符总数

[root@centous1 soft]# cat f1
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
[root@centous1 soft]# wc -l f1
6 f1
[root@centous1 soft]# wc -w f1
39 f1
[root@centous1 soft]# wc -m f1
79 f1
[root@centous1 soft]# wc -c f1
79 f1

sort [options] file(s) 

常用选项  

-r 执行反方向(由上至下)整理 

-n 执行按数字大小整理  

-f 选项忽略(fold)字符串中的字符大小写  

-u 选项(独特,unique)删除输出中的重复行  

-t  c 选项使用c做为字段界定符  

-k  X 选项按照使用c字符分隔的X列来整理能够使用多次

[root@centous1 soft]# cat f1 | sort -r
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
[root@centous1 soft]# cat f1 | sort -n
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
[root@centous1 soft]# echo "1 2 3 4" >> f1
[root@centous1 soft]# cat f1
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4
[root@centous1 soft]# sort -u f1
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
[root@centous1 soft]# cat /etc/passwd | sort -t ":" -k 3n | tail -n 10
abrt:x:173:173::/etc/abrt:/sbin/nologin
saslauth:x:499:76:Saslauthd user:/var/empty/saslauth:/sbin/nologin
zabbix:x:500:500::/home/zabbix:/bin/bash
testuser:x:4321:0::/home/test:/bin/csh
text:x:4322:4322::/home/text:/bin/bash
user1:x:4323:4323::/home/user1:/bin/bash
user2:x:4324:4324::/home/user2:/bin/bash
testbash:x:4326:4326::/home/testbash:/bin/bash
basher:x:4327:4327::/home/basher:/bin/bash
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin

uniq命令:从输入中删除重复的前后相接的行 

uniq [OPTION]… [FILE]… 

-c: 显示每行重复出现的次数 

-d: 仅显示重复过的行 

-u: 仅显示不曾重复的行 连续且完全相同方为重复  

常和sort 命令一起配合使用: sort  userlist.txt  |  uniq -c

列:

统计当前连接本机的每个远程主机IP的连接数,并按从大 到小排序

netstat -nt | tr -s " " ";" | cut -d ";" -f5 | uniq -c | sort -n | head -n -2

diff

比较两个文件之间的区别 $ diff foo.conf-broken foo.conf-works 

5c5

 < use_widgets = no — >

 use_widgets = yes 

注明第5行有区别(改变)

diff 命令的输出被保存在一种叫做“补丁”的文件中  

使用 -u 选项来输出“统一的(unified)”diff格式文 件,最适用于补丁文件

原创文章,作者:forest,如若转载,请注明出处:http://www.178linux.com/30467

(0)
forestforest
上一篇 2016-08-08 16:16
下一篇 2016-08-08 16:16

相关推荐

  • 初学Linux之程序进程管理工具汇总

    pstree, ps, pidof, pgrep, top, htop, glances, pmap, vmstat, dstat, kill, pkill, job, bg, fg, nohup 

    2017-12-16
  • PHP5.4的变化关注—What has changed in PHP 5.4.x

    What has changed in PHP 5.4.x Most improvements in PHP 5.4.x have no impact on existing code. There are a few incompatibilities and new features that should be …

    Linux干货 2015-06-17
  • N26-第十周

    1、请详细描述CentOS系统的启动流程(详细到每个过程系统做了哪些事情)     看到同学的图不错拿来用用 ~ ~      2、为运行于虚拟机上的CentOS 6添加一块新硬件,提供两个主分区;  (1) 为硬盘新建两个主分区;并为其安装grub;  (2) 为硬盘的第一个主…

    2017-03-26
  • 第七周作业

    1、创建一个10G分区,并格式为ext4文件系统;   (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;   (2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳; [root@localhost ~]# fdis…

    Linux干货 2017-07-04
  • nginx的模块应用

    1.location的定义 (1)当location中午定义时,默认使用server中的相关定义。 (2)当locating中有了相关定义,此时则启用location中的定义的内容。 nginx -t检查配置文件是否有语法错误 nginx -s reload重载nginx (3)即使server中的root中的目录下有了/admin的目录,但是同时又在loc…

    Linux干货 2016-10-30
  • 编译安装——吐血经验,内附脚本

    程序包编译安装: 源码包:name-VERSION-release.src.rpm         rpm由源码包安装后,使用rpmbuild命令制作成二进制格式的rpm包,而后再安装        &nbsp…

    Linux干货 2016-08-24