Linux书签(08)用linux cat新建/查看/拷贝/合并文件与输出打印

楔子:作为一名经常开车的老司机,查看日志文件是一种家常便饭。Linux more 命令就是这样一道非常下饭的菜,学会用它查看日志文件,也许会让很多问题及时被发现和定位。

Linux cat 命令

 

Linux cat 命令,用于连接文件并打印到标准输出设备上。与 tac 相反,cat 是从第一行到最后一行连续显示在萤幕上。

 

语法格式

cat [-选参] [--help] [--version] fileName

选参说明

可选参数简写可选参数全称含义
-n--number由 1 开始对所有输出的行数编号
-b--number-nonblank和 -n 相似,只不过对于空白行不编号
-s--squeeze-blank当遇到有连续两行以上的空白行,就代换为一行的空白行
-v--show-nonprinting使用 ^ 和 M- 符号,除了 LFD 和 TAB 之外
-E--show-ends在每行结束处显示 $
-T--show-tabs将 TAB 字符显示为 ^I
-A--show-all等价于 -vET 选项
-e 等价于 -vE 选项
-t 等价于 -vT 选项
--help 显示帮助并退出
--version 显示版本信息并退出,没有指定文件或指定的文件是 -,则从标准输入读取

 

参考示例

01)创建新文件

[devuser@sh-k82 ~]$ cat >  test.txt << EOF
> hello cat
> cat cat cat
> it is not cat 
> 
> test
> 
> EOF

02)查看文件的内容

[devuser@sh-k82 ~]$ cat test.txt 
hello cat
cat cat cat
it is not cat 

test

03)查看文件的内容,并显示行数编号

[devuser@sh-k82 ~]$ cat -n test.txt 
     1	hello cat
     2	cat cat cat
     3	it is not cat 
     4	
     5	test
     6	

04)查看文件的内容,并添加行数编号后输出到另外一个文件中

[devuser@sh-k82 ~]$ cat -n test.txt > test2.txt
[devuser@sh-k82 ~]$ ll
total 8
-rw-rw-r-- 1 devuser devuser 98 Oct  9 22:41 test2.txt
-rw-rw-r-- 1 devuser devuser 49 Oct  9 22:32 test.txt
[devuser@sh-k82 ~]$ cat -n test2.txt 
     1	     1	hello cat
     2	     2	cat cat cat
     3	     3	
     4	     4	it is not that cat
     5	     5	
     6	     6	test
     7	     7	

05)复制文件

[devuser@sh-k82 ~]$ cat test.txt > test3.txt
[devuser@sh-k82 ~]$ ll
total 12
-rw-rw-r-- 1 devuser devuser 98 Oct  9 22:41 test2.txt
-rw-rw-r-- 1 devuser devuser 49 Oct  9 22:42 test3.txt
-rw-rw-r-- 1 devuser devuser 49 Oct  9 22:32 test.txt
[devuser@sh-k82 ~]$ cat -n test3.txt 
     1	hello cat
     2	cat cat cat
     3	
     4	it is not that cat
     5	
     6	test
     7	
[devuser@sh-k82 ~]$ 

06)清空文件内容

[devuser@sh-k82 ~]$ cat /dev/null test.txt > test.txt 
cat: test.txt: input file is output file
[devuser@sh-k82 ~]$ cat test.txt 
[devuser@sh-k82 ~]$ 

注:dev/null:在类 Unix 系统中,/dev/null 称空设备,是一个特殊的设备文件,它丢弃一切写入其中的数据(但报告写入操作成功),读取它则会立即得到一个 EOF。

而使用 cat $filename > /dev/null 则不会得到任何信息,因为我们将本来该通过标准输出显示的文件信息重定向到了 /dev/null 中。

使用 cat $filename 1 > /dev/null 也会得到同样的效果,因为默认重定向的 1 就是标准输出。如果你对 shell 脚本或者重定向比较熟悉的话,应该会联想到 2,也即标准错误输出。

如果我们不想看到错误输出呢?我们可以禁止标准错误 cat $badname 2 > /dev/null。

07)持续写入文件内容,碰到 END 符后自动结束并保存文件

[devuser@sh-k82 ~]$ cat > test.txt << END
> hello cat
> 
> 
> cat cat cat
> 
> 
> it is not that cat
> 
> test
> END
[devuser@sh-k82 ~]$ cat test.txt 
hello cat


cat cat cat


it is not that cat

test
[devuser@sh-k82 ~]$ 

08)对非空行进行编号,空行不编号进行输出打印

[devuser@sh-k82 ~]$ cat -b test.txt 
     1	hello cat


     2	cat cat cat


     3	it is not that cat

     4	test
[devuser@sh-k82 ~]$ 

09)有时候文件中空行会很多,需要将多个空行合并为一行进行输出(不会改变文件内容)

[devuser@sh-k82 ~]$ cat -s test.txt 
hello cat

cat cat cat

it is not that cat

test
[devuser@sh-k82 ~]$ 

10)在每行结束处显示 $

[devuser@sh-k82 ~]$ cat -E test.txt 
hello cat$
$
$
cat cat cat$
$
$
it is not that cat$
$
test$
[devuser@sh-k82 ~]$ 

11)合并多个文件内容到一个新文件

[devuser@sh-k82 ~]$ cat test2.txt test3.txt > test4.txt
[devuser@sh-k82 ~]$ ll
total 16
-rw-rw-r-- 1 devuser devuser  98 Oct  9 22:41 test2.txt
-rw-rw-r-- 1 devuser devuser  49 Oct  9 22:42 test3.txt
-rw-rw-r-- 1 devuser devuser 147 Oct  9 23:10 test4.txt
-rw-rw-r-- 1 devuser devuser  51 Oct  9 22:50 test.txt
[devuser@sh-k82 ~]$ cat test4.txt 
     1	hello cat
     2	cat cat cat
     3	
     4	it is not that cat
     5	
     6	test
     7	
hello cat
cat cat cat

it is not that cat

test

[devuser@sh-k82 ~]$ 

12)制作镜像文件。例如要制作软盘的镜像文件,将软盘放好后输入:

cat /dev/fd0 > OUTFILE

注:OUTFILE 指输出的镜像文件名

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值