上一篇我们讲到了cat的使用示例:https://www.cnblogs.com/WeiLian1024/p/11863057.html
本篇我们将继续延续Cat讲讲Tac
本文是我们讲讲Linux技巧和窍门系列的一部分,在本文中,我们将介绍cat命令和tac的一些基本用法。
Cat指令示例
Cat命令在Linux中的基本用法
Cat命令(Concatenate的缩写)是* nix系统中最常用的命令之一。该命令最基本的用法是读取文件并将其显示到stdout,标示在终端上显示文件的内容。
# cat file.txt
Calinux@Calinux ~ $ cat filel.txt
Calinux is best site for Linux
Calinux@Calinux ~ $
cat命令的另一种用法是将多个文件读取或组合在一起,以下示例所示。
#cat file1.txt file2.txt file3.txt> file-all.txt Calinux@Calinux~ $ cat filel.txt file2.txt file3.txt Calinux is best site for Linux Calinux is best site for Linux on the web. Calinux is best site for Linux on the pLanet.. Calinux@Calinux~$
该命令还可用于使用“>”Linux重定向运算符将多个文件连接(合并)为一个文件。
#cat file1.txt file2.txt file3.txt> file-all.txt Calinux@Calinux ~ $ cat filel.txt file2.txt file3.txt > file-all.txt Calinux@Calinux ~ $ cat file-all.txt Calinux is best site for Linux Calinux is best site for Linux on the web. Calinux is best site for Linux on the planet.. Calinux@Calinux ~ $
通过使用重定向程序,可以file-all.txt使用以下语法将新文件的内容添加到的底部。
#cat file4.txt >> file-all.txt Calinux@Calinux~$ cat file4.txt Calinux is best site for Linux in the Universe.... Calinux@Calinux ~$ cat file4.txt >> file-all.txt Calinux@Calinux~$ cat file-all.txt Calinux is best site for Linux Calinux is best site for Linux on the web. Calinux is best site for Linux on the planet.. Calinux is best site for Linux in the Universe... Calinux@Calinux~$
该命令可用于文件的内容复制到一个新的文件。新文件可以重命名为任意文件。例如,将文件从当前位置复制到/tmp/目录。
#cat file1.txt> /tmp/file1.txt Calinux@Calinux ~ $ cat filel.txt>/tmp/filel. txt Calinux@Calinux~$ Is -l /tmp/filel.txt -rw-r--r--l Calinux Calinux 31 Apr 6 14:12 /tmp/filel.txt Calinux@Calinux~$
将文件从当前位置复制到/tmp/目录并更改其名称。
#cat file1.txt> /tmp/newfile.cfg Calinux@Calinux ~$ cat filel1.txt>/tmp/newfile.cfg Calinux@Calinux ~$ Is -1 /tmp/newfile.cfg -rw-r--r--l Calinux Calinux 31 Apr 6 15:19 /tmp/newfilt.cfg Calinux@Calinux ~ $
cat命令有一种较少用法是使用以下语法创建一个新文件。编辑完成后,点击CTRL+D保存并退出新文件。
#cat> new_file.txt Calinux@Calinux ~ $ cat > new_file. txt this is my new file Calinux@Calinux ~ $ cat new_file. txt this is my new file Calinux@Calinux ~ $
为了给文件的所有输出行编号,包括空行,使用-n。
#cat -n file-all.txt |CalinuxeCalinux ~ $ cat -n file-all.txt 1 Calinux is best site for Linux 2 3 Calinux is best site for Linux on the web. 4 5 Calinux is best site for Linux on the planet.. 6 7 Calinux is best site for Linux in the Universe.... Calinux@Calinux ~$
要仅显示每个非空行的编号,使用-b。
Calinux@Calinux~ $ cat -b file-all.txt 1 Calinux is best site for Linux 2 Calinux is best site for Linux on the web. 3 Calinux is best site for Linux on the planet.. 4 Calinux is best site for Linux in the Universe.... Calinux@Calinux~ $
如何在Linux中使用Tac命令
另一方面,* nix系统中有一个鲜为人知且使用较少的命令是Tac。Tac实际上是cat命令的反向版本,该命令打印文件的每一行,从下一行开始,在上一行结束,直到计算机标准输出。
#tac file-all.txt Calinux@tecamint ~ $ tac file-all.txt This is number four This is number three This is number two This is number one Calinux@tecamint ~ $
该命令最重要的选项之一是-s开关,它根据文件中的字符串或关键字将文件的内容分隔开。
Calinux@Calinux~$ tac file-all. txt --separator”two” This is number three This is number four This is number one
接下来,tac命令最重要的用法是,它可以为调试日志文件提供很大的帮助,从而颠倒日志内容的时间顺序。
$ tac /var/log/auth.log Or to display the last lines $ tail /var/log/auth.log | tac
样本输出
Calinux@Calinux ~ $ tac /var/log/auth.log pr 6 16:09:01 Calinux CRON[17714]: pam_unix(cron:session): session closed for user root Apr 6 16:09:01 Calinux CRON[17714]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 6 16:05:01 Calinux CRON[17582]: pam_unix(cron:session): session closed for user root Apr 6 16:05:01 Calinux CRON[17583]: pam_unix(cron:session): session closed for user root Apr 6 16:05:01 Calinux CRON[17583]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 6 16:05:01 Calinux CRON[17582]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 6 16:00:01 Calinux CRON[17434]: pam_unix(cron:session): session closed for user root ....
Calinux@Calinux ~ $ tail /var/log/auth.log | tac Apr 6 16:09:01 Calinux CRON[17714]: pam_unix(cron:session): session closed for user root Apr 6 16:09:01 Calinux CRON[17714]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 6 16:05:01 Calinux CRON[17582]: pam_unix(cron:session): session closed for user root Apr 6 16:05:01 Calinux CRON[17583]: pam_unix(cron:session): session closed for user root Apr 6 16:05:01 Calinux CRON[17583]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 6 16:05:01 Calinux CRON[17582]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 6 16:00:01 Calinux CRON[17434]: pam_unix(cron:session): session closed for user root Apr 6 16:00:01 Calinux CRON[17434]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 6 15:55:02 Calinux CRON[17194]: pam_unix(cron:session): session closed for user root Apr 6 15:55:01 Calinux CRON[17195]: pam_unix(cron:session): session closed for user root ...
与cat命令相同,tac在处理文本方面做得很出色,但是在其他类型的文件中尤其应避免使用它,尤其是二进制文件或第一行表示将运行该文件的程序的文件。