Home  >  Article  >  Operation and Maintenance  >  How to read the last few lines in cat in linux

How to read the last few lines in cat in linux

WBOY
WBOYOriginal
2022-07-19 17:18:1116377browse

In Linux, you can use the cat command with the tail command to view the last few lines of the file; the cat command is used to connect files and print to the standard output device, and the tail command can be used to view the contents of the file. When tail When the parameter of the command is set to "-n", the contents of the specified lines at the end of the file can be displayed. The syntax is "cat specified file | tail -n displays the number of the last few lines".

How to read the last few lines in cat in linux

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

How to read the last few lines of cat in Linux

The cat (full English spelling: concatenate) command is used to connect files and print to the standard output device.

Syntax format

cat [-AbeEnstTuv] [--help] [--version] fileName

Parameter description:

  • -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 encountering more than two consecutive blank lines, replace them with one blank line.

  • -v or --show-nonprinting: Use the ^ and M- symbols except LFD and TAB.

  • -E or --show-ends : Show $ at the end of each line.

  • -T or --show-tabs: Display TAB characters as ^I.

  • -A, --show-all: Equivalent to -vET.

  • -e: Equivalent to the "-vE" option;

  • -t: Equivalent to the "-vT" option;

tail command can be used to view the contents of the file,

Command format:

tail [参数] [文件]

Parameters:

  • -f Loop reading

  • -q Do not display processing information

  • -v Display detailed processing information

  • -cc8f01a3f8889dcf657849dd45bc0fc4c The number of bytes displayed

  • ##-ne1bb43da1dc495f3a2319be5fe7b6df5 Display the last n lines of the file

The example is as follows

Combine the three commands to view line x to line Y of the file.

Syntax format 1: Starting from line X, display line Y. That is, display the Y row

cat filename | tail -n +X | head -n Y

Example: Display rows 1000 to 3000

cat requirements.txt | tail -n +3000 | head -n 1000

*Note the order of the two methods

Decomposition:

tail -n 1000: Display the last 1000 lines

  • tail -n 1000: Start displaying from line 1000, and display the following

  • head -n 1000: Display the first 1000 lines

  • Recommended learning:

    Linux video tutorial

The above is the detailed content of How to read the last few lines in cat in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Can stm32 run linux?Next article:Can stm32 run linux?