转:
前言
cat命令用于读取文本文件,并且能够显示行号、特殊字符等。
使用说明
-n 对每行进行编号,包括空行
-b 对每行进行编号,不包括空行
-s 压缩连续的空行
-A 显示特殊字符,换行符、制表符等
使用举例
cat 读取文本文件
1: [root@master lianxi]# cat f2: # Generated automatically from man.conf.in by the3: # configure script.
cat 从标准输入读取
通过管道读取其命令的输出,cmd | cat -
1: [root@master lianxi]# echo "hello world" | cat2: hello world3: [root@master lianxi]# echo "hello world" | cat -4: hello world
-A 显示特殊字符
换行符$、制表符^I
1: [root@master lianxi]# cat f2: # Generated automatically from man.conf.in by the3: # configure script. config4: [root@master lianxi]# cat -A f5: # Generated automatically from man.conf.in by the$6: # configure script.^Iconfig$
添加行号
-n 每行都添加行号,包括空行
1: [root@master lianxi]# cat -n f2: 1 # Generated automatically from man.conf.in by the3: 24: 3 # configure script. config-b 除了空行,其他每行都加上行号
1: [root@master lianxi]# cat -b f2: 1 # Generated automatically from man.conf.in by the3:4: 2 # configure script. config-s 压缩连续空行
如下,该文件有3个连续的空行,被压缩成了一个
1: [root@master lianxi]# cat -n f2: 1 # Generated automatically from man.conf.in by the3: 24: 35: 46: 5 # configure script. config7: [root@master lianxi]# cat -s -n f8: 1 # Generated automatically from man.conf.in by the9: 210: 3 # configure script. config合并文件
cat f1 f2
1: [root@master lianxi]# cat f2: # Generated automatically from man.conf.in by the3:4:5:6: # configure script. config7: [root@master lianxi]# cat f18: # Generated automatically from man.conf.in by the9: # configure script. config10: [root@master lianxi]# cat f f111: # Generated automatically from man.conf.in by the12:13:14:15: # configure script. config16: # Generated automatically from man.conf.in by the17: # configure script. configcat f1 – f2 - 表示从标准输入读取
cat 是一次读取多个文件,先读取f1,再从 – (标准输入读取),在读取f2
1: [root@master lianxi]# cat f2: lwg3: [root@master lianxi]# cat f14: yangzhen5: [root@master lianxi]# echo "love" | cat f - f16: lwg7: love8: yangzhen
特殊说明
1)cat读取的是文本文件,读取文件文件肯定会因为无法“正确”解码而出现乱码
2)cat 读取文本文件的时候,是按照系统默认的编码方式来进行解码的。如果文本文件本身编码和默认编码不一致,可能也会出现乱码。可以先通过iconv命令来进行转码
3)cat命令读取多个是顺序依次读取的
总结
用cat命令来读取文本文件,也是linux中最常用的命令之一。