How to view certain lines of a file in Linux

青灯夜游
Release: 2022-04-15 16:56:53
Original
27794 people have browsed it

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".

How to view certain lines of a file in Linux

#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
Copy after login

You 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
Copy after login

How to view certain lines of a file in Linux

##Method 2: Use cat, tail and head commands

    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 x-th to Y-th 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
Copy after login

Example: Display rows 1000 to 3000

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

*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

  • Related recommendations: "

    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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template