Get the App
SLTechnology News&Howtos  ›  Servers  › 

How does linux view certain lines of a file

Shulou Source: shulou.com Published: 2022-05-31 10:48:51 09月30日 Update

This article mainly explains the "linux how to view some lines of the file", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "linux how to view a few lines of the file" bar!

Linux to view a few lines of the file method: 1, use the sed command, syntax "sed-n'X Yp' file name", you can view the contents of the file line X to Y; 2, with cat, tail and head commands, syntax "cat file name | tail-n + X | head-n Y".

The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

The method of viewing certain lines of a file by linux

Method 1: use the sed command

Sed-n'X Yp' filename

You can view lines x to Y of the file.

For example: display file lines 31 to 45

Sed-n'31 450p 'requirements.txt

Method 2: use the 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 with the specified parameters

The head command displays the beginning of the file to the standard output

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

Syntax format 1: start at line X and display line Y. That is, the line X~ (Ymur1) is displayed.

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

Example: start at line 3000 and display 1000 lines; that is, display 3000mm 3999 lines

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

Syntax format 2: display lines X to Y

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

Example: display lines 1000 to 3000

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

* Note the order of the two methods

Decompose:

Tail-n 1000: displays the last 1000 lines

Tail-n + 1000: displays from line 1000 and after line 1000

Head-n 1000: displays the first 1000 lines

Thank you for your reading, the above is the content of "linux how to view certain lines of the document", after the study of this article, I believe you have a deeper understanding of how to view a few lines of the linux file, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: File command method content grammar study file name standard format example output three lieutenant general parameter namely beginning idea situation tutorial text Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Linux macOS NVidia Shulou Tech Info