Shell commands - cat and tac for file and content processing

Posted by aeonsky on Thu, 09 May 2019 13:45:03 +0200

File and Content Processing - cat, tac

1. cat: Display file content

Functional description of cat command

The cat command is used to connect multiple files and print to screen output or redirect to a specified file

Grammatical Format of cat Command

cat [OPTION]... [FILE]...
cat [-AbeEnstTuv] [--help] [--version] fileName

Option description of file command

There are many cat parameters, but only a few are used. Table 1 shows the parameters and descriptions of the cat command:

Table 1: Parameters and description of cat command

Parameter options interpretative statement
- n or -- number Number all output lines starting from 1.
- b or -- number-nonblank Similar to - n, except that blank lines are not numbered.
- s or -- squeeze-blank When there are more than two blank lines in a row, replace them with blank lines in a row.
- v or -- show-nonprinting Use ^ and M - symbols, except for LFD and TAB.
- E or -- show-ends Display $at the end of each line.
- T or -- show-tabs Display the TAB character as ^ I.
-A, --show-all Equivalent to - vET.
-e Equivalent to the "-vE" option;
-t Equivalent to the "-vT" option;

Practical operation of cat command

Example 1: Execute the complete command of the cat command to generate the contents of the oldboy.txt file and view the contents without parameters

--------------------------------------------------------------
==>The order is as follows<==
--------------------------------------------------------------
cat >oldboy.txt <<EOF
I Love Linux

Me too

good
EOF

--------------------------------------------------------------
==>The demonstration is as follows<==
--------------------------------------------------------------
[root@oldboyedu /test]# ls
[root@oldboyedu /test]# cat >oldboy.txt <<EOF
> I Love Linux
>
> Me too

>good
> EOF
[root@oldboyedu /test]# ls
oldboy.txt

--------------------------------------------------------------
==>View content without parameters<==
--------------------------------------------------------------
[root@oldboyedu /test]# cat oldboy.txt 
I Love Linux

Me too

good

Example 2: Execute the cat command with - n and - b options, and compare the differences

--------------------------------------------------------------
==>-n Print line numbers, including blank lines<==
--------------------------------------------------------------
[root@oldboyedu /test]# cat -n oldboy.txt
     1  I Love Linux
     2  
     3  Me too
     4  
     5  good

--------------------------------------------------------------
==>-b Line numbers are printed, but blank lines are not included.<==
--------------------------------------------------------------
[root@oldboyedu /test]# cat -b oldboy.txt
     1  I Love Linux

     2  Me too

     3  good

Example 4: Execute the cat command with the - E parameter

[root@oldboyedu /test]# cat -E oldboy.txt
I Love Linux$
$
Me too$
$
good$

--------------------------------------------------------------
==>End identifier $,Even empty lines should be displayed $<==
--------------------------------------------------------------
[root@oldboyedu /test]# echo >oldboy1.txt
[root@oldboyedu /test]# cat -E oldboy1.txt
$
[root@oldboyedu /test]# ll oldboy1.txt
-rw-r--r-- 1 root root 1 4 Month 1415:18 oldboy1.txt

Example 5: Execute the cat command with - s parameters

--------------------------------------------------------------
==>A few additional lines of text<==
--------------------------------------------------------------
[root@oldboyedu /test]# cat >>oldboy.txt <<EOF
> 
> 
> if you like my blog
> 
> 
> call me qq
> EOF

--------------------------------------------------------------
==>give the result as follows<==
--------------------------------------------------------------
[root@oldboyedu /test]# cat oldboy.txt 
I Love Linux

Me too

good

if you like my blog

call me qq

--------------------------------------------------------------
==>-s The effect is as follows<==
--------------------------------------------------------------
[root@oldboyedu /test]# cat -s oldboy.txt
I Love Linux

Me too

good

if you like my blog

call me qq

2. tac: Reverse display of file content

Functional description of tac command

tac is the reverse spelling of cat, so the function of the command is to display the file content in reverse.

Grammatical Format of cat Command

tac [OPTION]... [FILE]...
tac [-AbeEnstTuv] [--help] [--version] fileName

Option description of tac command

tac is not used much, just a brief understanding here.

Example 1: View text content in reverse order

--------------------------------------------------------------
==>Write text in a document<==
--------------------------------------------------------------
[root@oldboyedu /test]# echo "1234567" >> oldboy3.txt
[root@oldboyedu /test]# echo "1234567" >> oldboy3.txt
[root@oldboyedu /test]# echo "1111111" >> oldboy3.txt
[root@oldboyedu /test]# echo "2222222" >> oldboy3.txt
[root@oldboyedu /test]# echo "3333333" >> oldboy3.txt

--------------------------------------------------------------
==>Normal view<==
--------------------------------------------------------------
[root@oldboyedu /test]# cat oldboy3.txt
1234567
1234567
1111111
2222222
3333333

--------------------------------------------------------------
==>Reverse lookup<==
--------------------------------------------------------------
[root@oldboyedu /test]# tac oldboy3.txt
3333333
2222222
1111111
1234567
1234567
[root@oldboyedu /test]# 

So far as I'm concerned, if you have any questions or mistakes, please feel free to comment on them.

Topics: Linux