A Linux command a day (10): cat command

post thumb
Devops
by Admin/ on 22 Sep 2021

A Linux command a day (10): cat command


The purpose of the cat command is to concatenate files or standard input and print them. This command is often used to display the contents of a file, or to concatenate several files for display, or to read the contents from standard input and display them, and it is often used in conjunction with the redirect symbol.

1. Command format.

cat [option ] [file ]…

2. Command functions.

cat has three main functions:

  1. display the entire file at once: cat filename

  2. create a file from the keyboard: cat > filename can only create new files, not edit existing files.

  3. merge several files into one file: cat file1 file2 > file

3. Command parameters.

-A, --show-all is equivalent to -vET

-b, --number-nonblank Numbering of non-blank output lines

-e is equivalent to -vE

-E, --show-ends shows $ at the end of each line

-n, --number Number all lines of output, starting with 1 for all lines of output

-s, --squeeze-blank Substitute a blank line for a blank line if there are more than two consecutive blank lines 

-t is equivalent to -vT

-T, --show-tabs displays skip characters as ^I

-u (ignored)

-v, --show-nonprinting uses ^ and M- references, except for LFD and TAB

4. Examples of use.

Example 1: Enter the contents of log2012.log with line numbers into the file log2013.log

Command:

cat -n log2012.log log2013.log

output:

[root@localhost test]# cat log2012.log   
2012-01  
2012-02  
======  
[root@localhost test]# cat log2013.log   
2013-01  
2013-02  
2013-03  
======  
[root@localhost test]# cat -n log2012.log log2013.log   
1 2012-01  
2 2012-02  
3  
4  
5 ======  
6 2013-01  
7 2013-02  
8  
9  
10 2013-03  
11 ======  
[root@localhost test]#  

Example 2: Append the contents of log2012.log and log2013.log to log.log after adding line numbers (blank lines are not added).

Command:

cat -b log2012.log log2013.log log.log

output:

[root@localhost test]# cat -b log2012.log log2013.log log.log  
1 2012-01  
2 2012-02  
3 ======  
4 2013-01  
5 2013-02  
6 2013-03  
7 ======  
[root@localhost test]#  

Example 3: Enter the contents of log2012.log with line numbers into the log.log file

Command:

output:

[root@localhost test]# cat log.log   
[root@localhost test]# cat -n log2012.log > log.log  
[root@localhost test]# cat -n log.log   
1 2012-01  
2 2012-02  
3  
4  
5 ======  
[root@localhost test]#  

Example 4: Using here doc to generate files

output:

[root@localhost test]# cat >log.txt <<EOF  
\> Hello  
\> World  
\> Linux  
\> PWD=$(pwd)  
\> EOF  
[root@localhost test]# ls -l log.txt   
-rw-r--r-- 1 root root 37 10-28 17:07 log.txt  
[root@localhost test]# cat log.txt   
Hello  
Hello  
Linux  
PWD=/opt/soft/test  
[root@localhost test]#  

Description:

Note the bolded part, where doc can do string substitution.

Remarks.

tac (reverse listing)

Commands:

tac log.txt

output:

[root@localhost test]# tac log.txt   
PWD=/opt/soft/test  
Linux  
World  
Hello  

Explanation:

tac is the reverse of cat, so its function is the opposite of cat. cat is displayed on the screen from the first line to the last line continuously, while tac is displayed on the screen from the last line to the first line in reverse!

Reference:

www.cnblogs.com/peida/archive/2012/10/30/2746968.html

comments powered by Disqus