您的位置:首页 > 运维架构 > Linux

LINUX文本处理工具讲解

2016-08-05 18:19 375 查看
在学习Linux系统中,我们要想看文件,一般就cat命令执行,执行大文件时,就不好使了;
less和more可以实现你心中的梦想,可以帮你查看大文件内容,
more命令:
[root@localhost ~]# more /etc/man_db.conf
#
#
# This file is used by the man-db package to configure the man and cat paths.
# It is also used to provide a manpath for those without one by examining
# their PATH environment variable. For details see the manpath(5) man page.
#
空格键:代表向下翻一页
enter:代表向下翻一行
/字符串:代表在这个显示的内容当中,向下查询“字符串”这个关键字
:f :立即显示文件名以及目前显示的行数;
q: 代表立刻离开more,不在显示该文本内容。
b:代表往回翻页。

less命令:
lessd的用法比起more又更加弹性,在more我们没办法往前翻,只能往后看,然后自动退出,man命令就是调用less命令来实现的
[root@localhost ~]# less /etc/rc.d/init.d/functions
空格键:向下翻动一页
k:翻下一行
j:向上翻一行
/字符串:向下查询
?字符串:向上查询
n:正向
N:反向
q:离开

文本截取内容head和tail命令
head [-n number] 文件
-n:后面接数字,代表几行的意思
[root@localhost ~]# head /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
默认情况为10行,显示。

[root@localhost ~]# head -n 20 /etc/man_db.conf
这是显示20行

tail命令:
tail [-n number] 文件
-n:后面接数字,代表显示后面几行的意思
-f:表示持续检测后面接的文件名,要Ctrl-c才会结束tail的检测
[root@localhost ~]# tail /etc/passwd
qemu:x:107:107:qemu user:/:/sbin/nologin
chrony:x:990:985::/var/lib/chrony:/sbin/nologin
当我们不跟数字时,默认从文件尾部输出10行,
要是想看文件最后3行,可以这样操作
[root@localhost ~]# tail -n 3 /etc/passwd
ntp:x:38:38::/etc/ntp:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
aliyun:x:1000:1000:aliyun:/home/aliyun:/bin/bash

cut命令:剪切文本的字符段,达到自己想要的结果

cut [OPTION]... [FILE]...
-d DELIMITER: 指明分隔符,默认tab
-f FILEDS:
#: 第#个字段
#,#[,#]:离散的多个字段,例如1,3,6
#-#:连续的多个字段, 例如1-6
混合使用:1-3,7
-c按字符切割
--output-delimiter=STRING指定输出分隔符
例:找出/etc/passwd文件中的uid;首先我们分析,可以用冒号:作为分隔符,这样可以分出7个字段,然后我们取出第3个字段,表示的就是uid;
[root@localhost ~]# cut -d : -f 3 < /etc/passwd
0
1
2
取出第3,4,7段时,应该这样表达;-d : -f3,4,7

[root@localhost ~]# cat /etc/passwd | cut -d: -f3,4,7
0:0:/bin/bash
1:1:/sbin/nologin
2:2:/sbin/nologin
取出连续3,567,可以用。-d : -f 3,5-7
[root@localhost ~]# cat /etc/passwd | cut -d: -f3,5-7
0:root:/root:/bin/bash
1:bin:/bin:/sbin/nologin
要是按字符切割:表示的就是从行,最前面,到的第几个字符
[root@localhost ~]# cat txt
123456789
123456789
123456789
[root@localhost ~]# cat txt | cut -c 5
替换符号,,把取出来的中间换上*号

[root@localhost ~]# cut -d: -f3,4 --output-delimiter=* < /etc/passwd
0*0
1*1
2*2
paste (合并两个文件同行号的列到一行)
-d:分隔符:指定分隔符,默认用tab
-s:所有行合成一行显示
paste f1 f2
paste -s f1 f2
[root@localhost ~]# paste -d "+" -s txt txt1
123456789+ 123456789+ 123456789
121121212121+232323232323+44444444444455+66666666677777
指明分隔符为"+"然后每个文件所有行都合成一行。
wc命令:收集文本的统计数据(行,字节,单词)
[root@localhost ~]# cat /etc/passwd | wc -l
44 行
[root@locahost ~]# cat /etc/passwd |wc -w
89 单词
[root@localhost ~]# cat /etc/passwd | wc
44 89 2331
[root@localhost ~]# cat /etc/passwd | wc -c
2331 字节、

文本排序sort命令:
sort[options] file
-r :执行反方向
-n:安数字大小
-f:忽略字符串中的字符大小写
-u:选项删除输出中的重复行
-t c 选项使用c作为字段定界
-k X 选项按照使用c字符分隔的X列来整理能使用多次

uniq命令:从输入中删除重复的前后相接的行
uniq [option] ...[file]...
-c:显示每行重复出现的次数
-d:仅显示重复过的行
-u:仅显示不曾重复的行;连续且完全相同为为重复

常和sort命令一起配合使用:
sort userlist.txt | uniq -c
[root@localhost ~]# sort -r txt | uniq -c
4 ssssss
1 eeeeee
1 dddddd
4 aaaaaa

diff命令比较两个文件的不同之处;
[root@localhost ~]# diff -u f1 f2 > diff.log打个补丁输出到diff。log文件
[root@localhost ~]# rm -f f2
[root@localhost ~]# patch -b f1 diff.log
patching file f1
[root@localhost ~]# mv f1.orig f1
[root@localhost ~]#
[root@localhost ~]# mv f1 f2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  package provide details