Linux:: File content operation [6]: Specify the middle part of the file (line number to line number) to output and preliminary understanding of the line content pipeline

Preface: This article is the content of the basic Linux operation chapter!
The environment I use is based on Tencent Cloud Server: CentOS 7.6 64bit.


Study set:


Note: This article involves viewing or editing file content, so there are pre-prepared test examples. If the reader does not have a test example, you can refer to the directory in point 0 to generate it!


Directory index:
0. Test the use case command
1. Review the meaning
of the instruction and ask questions 2. Propose ideas to solve the problem
3. Implementation: output the content of the specified middle line in the file
3.1 Method 1 (output redirection method)
3.2 Method 2 (combination) Pipeline): A brief introduction to pipeline
4. Specified output of local content in the middle
5. Recommended related articles or series


0. Test using case commands

count=0; while [ $count -le 100 ]; do echo "hello ${count}"; let count++; done > file.txt
  • The above command is used to generate hello x {x: 1~100} to generate 100 lines of data and write it to a file! [Note: Do not modify the command format! The writing method will be explained later!

1. Review the meaning of the instruction and ask questions

Review instructions:

  • "cat / tac": View the entire contents of the target file in forward/reverse order!
  • "more / less": View part or all of the target file!
  • "head / tail": View the contents of n lines before and after | Specify to view part of the file!

Ask a question:
We can use known instructions to view the contents of n lines before and after. If in a file with a large amount of data (more than 1000 lines), I want to view: What should I do if I want to view the contents of lines 50 ~ 75?


2. Put forward ideas to solve the problem

General idea: We can first find out the first 75 lines of content, and then check the last 26 lines of content among the 75 lines of content. Note: The content of lines 50 ~ 75: It belongs to the left-closed and right-closed interval of the interval, and the difference between the intervals needs +1!

Method 1 (output redirection method): We can use: head -75 [file]: find out the first 75 lines of content, store it in a temporary file through output redirection, and specify the output tail -26 [temporary file] in the temporary file.

Method 2 (combined with pipeline): This method will introduce the concept of a pipeline. Simply put, the function of the pipeline is to transfer the execution result of the instruction on the left side of the pipeline to the instruction on the right side of the pipeline for continued execution!


3. Implementation: Output the content of the specified middle line in the file

3.1 Method 1 (output redirection method)

Method 1 (output redirection method): We can use: head -75 [file]: find out the first 75 lines of content, store it in a temporary file through output redirection, and specify the output tail -26 [temporary file] in the temporary file. [Note: The implementation form is not unique!

/* 1. 先将前 head + 输出重定向把 75 行内容输出到 temp.txt 文件中 */
[Mortal@VM-12-16-centos test_file]$ head -75 file.txt > temp.txt
[Mortal@VM-12-16-centos test_file]$ ll
total 8
-rw-rw-r-- 1 Mortal Mortal 900 Jun  5 21:14 file.txt
-rw-rw-r-- 1 Mortal Mortal 665 Jun  5 21:15 temp.txt

/* 2. 再使用 tail 指令输出 temp.txt 文件中的后 26 行内容 */
[Mortal@VM-12-16-centos test_file]$ tail -26 temp.txt
hello 49
hello 50
hello 51
hello 52
hello 53
hello 54
hello 55
hello 56
hello 57
hello 58
hello 59
/* 此处笔者删除了部分内容!防止文章篇幅过长! */


3.2 Method 2 (combined with pipelines): briefly introduce pipelines

  • Pipe " | ": Simply put, the function of the pipe is to hand over the execution results of the instructions on the left side of the pipe to the instructions on the right side of the pipe for continued execution! [Currently you just need to know how to use it! Will explain in detail later!

  • Pipeline: Data can be processed in batches and in a streamlined manner! In the following process the pipeline will generate the pipeline file! But it is a memory-level file, not on disk!

[Mortal@VM-12-16-centos test_file]$ ls
file.txt  temp.txt  tmp.txt
[Mortal@VM-12-16-centos test_file]$ rm temp.txt tmp.txt 
[Mortal@VM-12-16-centos test_file]$ ls
file.txt
[Mortal@VM-12-16-centos test_file]$ head -75 file.txt | tail -26 
hello 49
hello 50
hello 51
hello 52
hello 53
hello 54
hello 55
hello 56
hello 57
hello 58
hello 59
hello 60
hello 61
hello 62
hello 63
hello 64
hello 65
/* 此处笔者删除了部分内容!防止文章篇幅过长! */

Insert image description here


4. Recommendations for related articles or series

1. Linux learning directory collection ;


2. Linux:: [Basic commands:: File content operations: (1)]:: nano command:: Use the built-in file editor and a simple demonstration to compile and execute an executable program with gcc under Linux (for understanding only: it will be used Just create a file) [Basically not used] ;
3. Linux:: [Basic commands:: File content operations: (2)]:: cat / tac command:: View the entire contents of the target file in forward/reverse order And additional usage of cat: read content from standard input and output ;
4.Linux:: [Basic instructions:: File content operations: (3)]:: more / less instructions:: View part or all of the contents of the target file | Specify to view the first n lines of content [compared to the two, it is recommended to use less] ;
5. Linux:: [Basic commands:: File content operations: (4)]:: head / tail command:: Specify to view part of the file content | View the content of the first n lines
6. Linux:: File content operation [5]: Simple usage of the echo command and input redirection, output redirection, and append redirection in writing file content!


Guess you like

Origin blog.csdn.net/weixin_53202576/article/details/131056043