转: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. c#中获得MD5字符串方法

    在用户登录的过程中,我们会遇到要查询对比用户名密码的是否存在或者是否正确,但是数据库中存放的是通过MD5加密的字符串,所有我们可以先把用户输入的用户名或者是密码先转为DM5字符串再跟数据库查出的MD5 ...

  2. Mysql入门的10条语句

    1.查看当前服务器下面,有那些库(database)? 答: show databases; 2.如何创建一个数据库? 答 : create database 最后面在写数据库名:分号结束 3.选择数 ...

  3. 第1张 Maven简介 学习笔记

    什么是构建? 编译.运行单元测试.生成文档.打包和部署 Maven的应用: 构建工具 依赖管理工具 通过坐标系统定位到每一个构建(artifact) 项目信息管理工具 Maven对于项目目录结构.测试 ...

  4. Codeforces Round #325 (Div. 2) A

    A. Alena's Schedule time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. YUY格式

    YUV格式有两大类:planar和packed. 对于planar的YUV格式,先连续存储所有像素点的Y,紧接着存储所有像素点的U,随后是所有像素点的V. 对于packed的YUV格式,每个像素点的Y ...

  6. 读入输出优化_C++

    当我们考试时遇到大量的读入或者输出时,这些代码会耗费许多运行程序的时间,导致TL 本来 log2n 的算法因为读入被卡成线性的就太不划算了,所以我们这里要采用读入输出优化 getchar 和 putc ...

  7. 使用matlab判断男声与女声

    (转自) http://wenku.baidu.com/view/1d55480fbe1e650e52ea99a3.html %filename:manwoman.m %different man f ...

  8. Linux网络编程一步一步学【转】

    转自:http://blog.chinaunix.net/uid-10747583-id-297982.html Linux网络编程一步一步学+基础  原文地址:http://blogold.chin ...

  9. http请求分析

    一个Http请求一般始于如下几种方式: 1.在浏览器中输入一个URL地址 2.网页中的一个超链接 3.Response.Redirect("http://www.sohu.com" ...

  10. 4.创建OpenStack的node环境脚本

    创建OpenStack的node环境脚本 使用source admin-openrc.sh来运行脚本 在任意目录下创建admin-openrc.sh文件 vim ~/admin-openrc.sh e ...