How to view certain lines of a file in Linux: 1. Use the sed command with the syntax "sed -n 'X,Yp' file name" to view the contents of lines X to Y of the file; 2. Use the cat, tail and head commands with the syntax "cat file name | tail -n X | head -n Y".
#The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.
How to view certain lines of a file in Linux
##Method 1: Use the sed command
sed -n 'X,Yp' filename
sed -n '31,45p' requirements.txt
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: Display starting from line 1000 and display the following lines
head -n 1000: Display the first 1000 lines
Linux Video Tutorial》
The above is the detailed content of How to view certain lines of a file in Linux. For more information, please follow other related articles on the PHP Chinese website!