Use Head and Tail to read text streams in Block Mode

Source: Internet
Author: User
Tags apache access log
Article Title: use Head and Tail to read text streams in blocks. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
Jacek introduces head and tail commands in this article, which are useful for block-based processing of data in static and dynamic files.
  
Suppose you want to process only a part of the file, such as the first few or the last few lines, what should you do? Use head (which sends the first 10 rows to the standard output) or tail (which sends the last 10 rows to the standard output ).
  
You can use the-n option to change the number of lines that these commands send to their standard output (of course, the output result varies with the content of the XF86Config file ):
  
Listing 1. Send the content of the selected number of lines in XF86Config to the standard output $ head-n 4/etc/X11/XF86Config
  
# File generated by anaconda.
  
#*************************************** *******************************
  
# Refer to the XF86Config (4/5) man page for details about the format
  
# This file.
  
  
$ Tail-n 4/etc/X11/XF86Config
  
Modes "1600x1200"
  
ViewPort 0 0
  
EndSubsection
  
EndSection
  
What if you want the head or tail to be in bytes instead of behavior units? You can replace the-n option with the-c option. Therefore, to display the first 200 characters, use head-c 200 file or tail-c 200 file to display the last 200 characters. If the number is followed by B (block), the number is multiplied by 512. Similarly, k (representing a kilobytes (kilobyte) indicates that 1024 is used to multiply the given number, and m (representing a megabyte) indicates that )) it indicates that the given number is multiplied by 1048576 bytes.
  
Remember that there is a major difference between head file1 file2 file3 and cat file1 file2 file3 | head. The former prints the content of the specified number of lines in each file. The content of different files is separated by header information, with the header information starting with => followed by the file name. The latter prints the content of the specified number of rows in the input stream consisting of the files listed after the cat command, but treats the input stream as a single file. You can use the-q (indicating silent (quiet) option to disable the file name header. -V (verbose) is the opposite of-q )).
  
Assume that the file you want to process has been changing during the processing period (for example, this is the case when you ask the head or tail to read data from the file being written by another command ), use the-f option to allow tail to continuously read data from the specified file and send the data to its own standard output. This option is ignored when data is sent through an MPS queue. Therefore, cat file | tail-f won't get the expected results, but tail-f file can.
  
(If tail is reading more than one file, the contents of each row are separated by standard header information to indicate which file they are from. The standard header information starts with => .)
  
This option is not suitable for monitoring system logs. For example, tail-f/var/log/access is executed in a separate terminal window (or a separate console. log will continue to print the newly added Apache access log entries after each click until you stop it with Ctrl-C.
  
By combining head and tail, you can read a piece of data with a given length from the middle of the file! The following describes how to do this: assume that you want to read 1,000th bytes of data starting from the beginning of the file. You can use cat file | head-c 1788 | tail-c 789 to solve this problem.
  
   Sort files in reverse order using the tac command
If you want to sort the rows in the file in reverse order, what should you do? This requires the tac command. (Note that tac is spelled by cat in reverse order .) This command sorts the lines or segments of the listed files in reverse order.
  
This command cannot sort the order of files in reverse order-you have to do this task yourself and list the files in reverse order after the tac command. As an example of how tac works, use ls-l | tail and ls-l | tail | tac in your home directory to compare the results.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.