Home >Operation and Maintenance >Linux Operation and Maintenance >How to read the first few lines of cat in linux
In Linux, you can use the cat command with the head command to view the first few lines of the file; the cat command is used to connect files and print to the standard output device, and the head command can display the beginning of the file to the standard output. , just set the parameter of the head command to "-n", and the syntax is "cat specifies the file | head -n displays the number of the first few lines".
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
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;
The cat command can be used to display the contents of a text file
The tail command outputs the last part of the file to a standard device according to the specified parameters
The head command displays the beginning of the file to the standard output
Combine the three commands to view the xth to Yth lines 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 tutorialThe above is the detailed content of How to read the first few lines of cat in linux. For more information, please follow other related articles on the PHP Chinese website!