Linux学习笔记4Cat命令

cat - concatenate files and print on the standard output
 --把文件连接后输出到屏幕上,其实就是查看文件内容。

SYNOPSIS
       cat 
[OPTION] [FILE]

DESCRIPTION
       Concatenate FILE(s)
, or standard input, to standard output.

       -A
, --show-all
              equivalent to -vET --等同与参数-vET

       -b
, --number-nonblank
              number nonblank output lines --对空白行不编号,与-n有相似。

       -e     equivalent to -vE --等同与参数-vE

       -E
, --show-ends
              display $ at end of each line --每行以$符号为行结尾

       -n
, --number
              number all output lines --由1开始对所有输出的行数编号

       -s
, --squeeze-blank
              never more than one single blank line --当遇到连续2行以上的空白行,只保留一行空白行

       -t     equivalent to -vT

       -T
, --show-tabs
              display TAB characters as ^I

       -u     (ignored)

       -v
, --show-nonprinting
              use ^ and M- notation
, except for LFD and TAB

练习:

[root@CentOS4 ok_008]# cat mytest
Hello world!


Hello world 1
Hello world 2


hello world 3

[root@CentOS4 ok_008]# cat -n mytest
     1  Hello world!
     2
     3
     4  Hello world 1
     5  Hello world 2
     6
     7
     8  hello world 3
[root@CentOS4 ok_008]# cat -b mytest
     1  Hello world!


     2  Hello world 1
     3  Hello world 2


     4  hello world 3
从以上命令我们可以验证 -n与 -b的差异。

[root@CentOS4 ok_008]# cat -b -E mytest
     1  Hello world!$
$
$
     2  Hello world 1$
     3  Hello world 2$
$
$
     4  hello world 3$
每行以$符号为行结尾

现在想合并mytest和oktest两个文件的内容成一个新的文件NewTest.txt
[root@CentOS4 ok_008]# ls -l
total 24
-rw-r--r--  1 root root   59 Sep 18 19:48 mytest
drwxr-xr-x  3 root root 4096 Sep 13 22:51 ok
-rw-r--r--  1 root root   21 Sep 17 20:45 oktest
[root@CentOS4 ok_008]# cat mytest oktest >> newtest.txt
[root@CentOS4 ok_008]# ls -l
total 32
-rw-r--r--  1 root root   59 Sep 18 19:48 mytest
-rw-r--r--  1 root root   80 Sep 18 19:53 newtest.txt
drwxr-xr-x  3 root root 4096 Sep 13 22:51 ok
-rw-r--r--  1 root root   21 Sep 17 20:45 oktest
查看新的文件newtest.txt的内容:
[root@CentOS4 ok_008]# cat -b newtest.txt
     1  Hello world!


     2  Hello world 1
     3  Hello world 2


     4  hello world 3
     5  this is a test file.

合并两个文件的内容

[root@CentOS4 ok_008]# cat mytest oktest > newtest1.txt
[root@CentOS4 ok_008]# ls -l
total 40
-rw-r--r--  1 root root   59 Sep 18 19:48 mytest
-rw-r--r--  1 root root   80 Sep 18 19:56 newtest1.txt
-rw-r--r--  1 root root   80 Sep 18 19:53 newtest.txt
drwxr-xr-x  3 root root 4096 Sep 13 22:51 ok
-rw-r--r--  1 root root   21 Sep 17 20:45 oktest
[root@CentOS4 ok_008]#  cat -b newtest1.txt
     1  Hello world!


     2  Hello world 1
     3  Hello world 2


     4  hello world 3
     5  this is a test file.

我们仔细会发现cat mytest oktest > newtest1.txt 和 cat mytest oktest >> newtest.txt 返回的结果在这里相同。
其实根据《Linux常用命令全集》的例子:
cat -n textfile1 > textfile2 把 textfile1 的档案内容加上行号后输入 textfile2 这个档案里
cat -b textfile1 textfile2 >> textfile3 把 textfile1 和 textfile2 的档案内容加上行号(空白行不加)之后将内容附
可以看出两个命令的差异,这里就不多写例子。

清空某个文件的内容:

[root@CentOS4 ok_008]# cat /dev/null > mytest
[root@CentOS4 ok_008]# cat -b mytest
[root@CentOS4 ok_008]# ls -l
total 52
-rw-r--r--  1 root root    0 Sep 18 20:05 mytest
-rw-r--r--  1 root root   80 Sep 18 19:56 newtest1.txt
-rw-r--r--  1 root root   93 Sep 18 20:01 newtest3.txt
-rw-r--r--  1 root root   93 Sep 18 20:01 newtest4.txt
-rw-r--r--  1 root root   80 Sep 18 19:53 newtest.txt
drwxr-xr-x  3 root root 4096 Sep 13 22:51 ok
-rw-r--r--  1 root root   34 Sep 18 20:00 oktest
--明天找一下资料为什么要使用cat /dev/null > mytest能清空文件内容。

创建一个新的文件:
[root@CentOS4 ok_008]# cat >new.txt --按 CTRL + C 结束录入
[root@CentOS4 ok_008]# ls -l
total 56
-rw-r--r--  1 root root    0 Sep 18 20:05 mytest
-rw-r--r--  1 root root   80 Sep 18 19:56 newtest1.txt
-rw-r--r--  1 root root   93 Sep 18 20:01 newtest3.txt
-rw-r--r--  1 root root   93 Sep 18 20:01 newtest4.txt
-rw-r--r--  1 root root   80 Sep 18 19:53 newtest.txt
-rw-r--r--  1 root root    0 Sep 18 20:07 new.txt
drwxr-xr-x  3 root root 4096 Sep 13 22:51 ok
-rw-r--r--  1 root root   34 Sep 18 20:00 oktest
温习以前的rm命令把一些刚才临时建立的文件(如newtest1.txt,newtest3.txt,newtest4.txt,newtest.txt,new.txt)删除掉:

[root@CentOS4 ok_008]# rm newtest1.txt newttest3.txt newtest4.txt newtest.txt
rm: remove regular file `newtest1.txt'? y
rm: cannot lstat `newttest3.txt': No such file or directory
rm: remove regular file `newtest4.txt'? y
rm: remove regular file `newtest.txt'? y
[root@CentOS4 ok_008]# rm newtest3.txt
rm: remove regular file `newtest3.txt'? y
[root@CentOS4 ok_008]# ls -l
total 24
-rw-r--r--  1 root root    0 Sep 18 20:05 mytest
-rw-r--r--  1 root root    0 Sep 18 20:07 new.txt
drwxr-xr-x  3 root root 4096 Sep 13 22:51 ok
-rw-r--r--  1 root root   34 Sep 18 20:00 oktest
[root@CentOS4 ok_008]# rm newt.txt
rm: cannot lstat `newt.txt': No such file or directory
[root@CentOS4 ok_008]# rm new.txt
rm: remove regular empty file `new.txt'? y
[root@CentOS4 ok_008]# ls -l
total 20
-rw-r--r--  1 root root    0 Sep 18 20:05 mytest
drwxr-xr-x  3 root root 4096 Sep 13 22:51 ok
-rw-r--r--  1 root root   34 Sep 18 20:00 oktest

今天就学到这里,休息一下,听完一首《Lonely》冲凉去,呵呵。


 

原文地址:https://www.cnblogs.com/wghao/p/897907.html