转:http://www.cnblogs.com/lwgdream/archive/2013/11/06/3409802.html

前言

cat命令用于读取文本文件,并且能够显示行号、特殊字符等。

使用说明

-n 对每行进行编号,包括空行

-b 对每行进行编号,不包括空行

-s 压缩连续的空行

-A 显示特殊字符,换行符、制表符等

使用举例

cat 读取文本文件

   1: [root@master lianxi]# cat f
   2: # Generated automatically from man.conf.in by the
   3: # configure script.

cat 从标准输入读取

通过管道读取其命令的输出,cmd | cat -

   1: [root@master lianxi]# echo "hello world" | cat
   2: hello world
   3: [root@master lianxi]# echo "hello world" | cat -
   4: hello world

-A 显示特殊字符

换行符$、制表符^I

   1: [root@master lianxi]# cat f
   2: # Generated automatically from man.conf.in by the
   3: # configure script.     config
   4: [root@master lianxi]# cat -A f
   5: # Generated automatically from man.conf.in by the$
   6: # configure script.^Iconfig$

添加行号

-n 每行都添加行号,包括空行

   1: [root@master lianxi]# cat -n f
   2:      1  # Generated automatically from man.conf.in by the
   3:      2
   4:      3  # configure script.     config

-b 除了空行,其他每行都加上行号

   1: [root@master lianxi]# cat -b f
   2:      1  # Generated automatically from man.conf.in by the
   3:  
   4:      2  # configure script.     config

-s 压缩连续空行

如下,该文件有3个连续的空行,被压缩成了一个

   1: [root@master lianxi]# cat -n f
   2:      1  # Generated automatically from man.conf.in by the
   3:      2
   4:      3
   5:      4
   6:      5  # configure script.     config
   7: [root@master lianxi]# cat -s -n f
   8:      1  # Generated automatically from man.conf.in by the
   9:      2
  10:      3  # configure script.     config

合并文件

cat f1 f2

   1: [root@master lianxi]# cat f 
   2: # Generated automatically from man.conf.in by the
   3:  
   4:  
   5:  
   6: # configure script.     config
   7: [root@master lianxi]# cat f1
   8: # Generated automatically from man.conf.in by the
   9: # configure script.     config
  10: [root@master lianxi]# cat f f1
  11: # Generated automatically from man.conf.in by the
  12:  
  13:  
  14:  
  15: # configure script.     config
  16: # Generated automatically from man.conf.in by the
  17: # configure script.     config

cat f1 – f2  - 表示从标准输入读取

cat 是一次读取多个文件,先读取f1,再从 – (标准输入读取),在读取f2

   1: [root@master lianxi]# cat f
   2: lwg
   3: [root@master lianxi]# cat f1
   4: yangzhen
   5: [root@master lianxi]# echo "love" | cat f - f1
   6: lwg
   7: love
   8: yangzhen

特殊说明

1)cat读取的是文本文件,读取文件文件肯定会因为无法“正确”解码而出现乱码

2)cat 读取文本文件的时候,是按照系统默认的编码方式来进行解码的。如果文本文件本身编码和默认编码不一致,可能也会出现乱码。可以先通过iconv命令来进行转码

3)cat命令读取多个是顺序依次读取的

总结

用cat命令来读取文本文件,也是linux中最常用的命令之一。

linux命令详解:cat命令的更多相关文章

  1. linux shell 脚本攻略学习14--head命令详解,tail命令详解

    当要查看上千行的大文件时,我们可不会用cat命令把整个文件内容给打印出来,相反,我们可能只需要看文件的一小部分地内容(例如文件的前十行和后十行),我们也有可能需要打印出来前n行或后n行,也有可能打印除 ...

  2. linux shell 脚本攻略学习13--file命令详解,diff命令详解

    一.file命令详解 find命令可以通过查看文件内容来找出特定类型的文件,在UNIX/ Linux系统中,文件类型并不是由文件扩展名来决定的(windows中却正是这么做的),file命令的目的是从 ...

  3. linux shell 脚本攻略学习 -- head命令详解, tail命令详解

    当要查看上千行的大文件时,我们可不会用cat命令把整个文件内容给打印出来,相反,我们可能只需要看文件的一小部分地内容(例如文件的前十行和后十行),我们也有可能需要打印出来前n行或后n行,也有可能打印除 ...

  4. [转]dd命令、cp命令详解+dd命令、cp命令对比 ---delong

    出处:http://blog.csdn.net/sun_app/article/details/18263299 1.dd命令详解 1)中文man手册dd的解释 NAME       dd - 转换和 ...

  5. linux命令大全之watch命令详解(监测命令运行结果)

    watch是一个非常实用的命令,基本所有的Linux发行版都带有这个小工具,如同名字一样,watch可以帮你监测一个命令的运行结果,省得你一遍遍的手动运行.在Linux下,watch是周期性的执行下个 ...

  6. linux命令详解:df命令

    转:http://www.cnblogs.com/lwgdream/p/3413579.html 前言 df命令用来查看系统的space和inode使用情况,也是常用命令之一 使用说明 -a 显示所有 ...

  7. linux系统find命令详解+xargs命令 、exec命令

    find 作用:查找文件 1.name: 指定文件名 例子1. 找到以du结尾的文件 ╭─root@localhost.localdomain ~ ╰─➤ find / -name "*du ...

  8. tar 命令详解 / xz 命令

    ]# tar [-cxtzjvfpPN] 文件与目录 ....参数:-c :建立一个压缩文件的参数指令(create 的意思):-x :解开一个压缩文件的参数指令!-t :查看 tarfile 里面的 ...

  9. top显示命令详解+top命令使用

    http://blog.csdn.net/u014226549/article/details/22041289

  10. Linux CAT与ECHO命令详解 <<EOF EOF

    Linux CAT与ECHO命令详解 cat命令是Linux下的一个文本输出命令,通常是用于观看某个文件的内容的: cat主要有三大功能: .一次显示整个文件. $ cat filename .从键盘 ...

随机推荐

  1. GCC特性之__init修饰解析 - kasalyn的专栏 - 博客频道 - CSDN.NET

    , GCC特性之__init修饰解析 - kasalyn的专栏 - 博客频道 - CSDN.NET.MathJax_Hover_Frame {border-radius: .25em; -webkit ...

  2. HttpRuntime.Cache再学习

    摘抄: 可以看到:读缓存,其实是在调用Get方法,而写缓存则是在调用Insert方法的最简单的那个重载版本. 注意了:Add方法也可以将一个对象放入缓存,这个方法有7个参数,而Insert也有一个签名 ...

  3. BI商业智能培训系列——(二)SSIS入门

    简介: SSIS,Microsoft SQL Server Integration Services.Integration意为"整合"."一体化".上篇博客中 ...

  4. redis单线程问题

    1.redis的单线程指的是什么单线程?同一个时间点只处理一个客户端的连接,也就是redis网络模块的单线程. 2.redis为什么设计成单线程 具体作者怎么想的,我不知道,我说一下我的理解(1)re ...

  5. weex 开发 (已放弃了)

    关于weex 开发 本菜已放弃使用了,当初选择使用weex 是为了同时支持h5 和 android / ios  三端:想法很不错,深入之后 突然发现,开发起来并没有很轻松,因为weex 中有些方法, ...

  6. Dubbo基础介绍

    基础知识 Dubbo是什么:Dubbo是一个分布式的服务框架,提供高性能和透明化的RPC远程调用方案,以及SOA服务治理方案 Dubbo涉及的知识: 远程调用:RMI.hassion.webservi ...

  7. hdu 6102 GCDispower

    多校6 GCDispower(容斥) 题意: 给一个长度为\(n\)的排列 给q组询问 每次查询\(L,R\)内的答案 \(\sum_{i=L}^{R}\sum_{j=i+1}^{R}\sum_{k= ...

  8. NOIP2017赛前模拟11月2日总结

    分数爆炸的一天··但也学了很多 题目1:活动安排 给定n个活动的开始时间与结束时间··只有一个场地··要求保留尽量多的活动且时间不冲突···场地数n<=100000 考点:贪心 直接将结束时间按 ...

  9. 《c程序设计语言》-2.6~2.8

    #include <stdio.h> unsigned setbits(unsigned x, int p, int n, unsigned y) { return (x & (( ...

  10. bzoj 1111 - 四进制的天平

    Description 给定 1000的十进制数, 求 最小的 四幂拆分 方案 有多少种 Solution 先大除法 \(n\log_4(n)\)次取余转化为 四进制数. 然后从 低位 往 高位 \( ...