Home >Operation and Maintenance >Linux Operation and Maintenance >How to view certain lines of a file in Linux
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' filenameYou can view lines x to Y of the file. For example: display lines 31 to 45 of the file
sed -n '31,45p' requirements.txt##Method 2: Use cat, tail and head commands
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!