Site icon LinuxCommands.site

How to fast search for specified content in large files in linux

When we use linux, we often encounter the situation of searching for specified content in large files.

In this case, we directly use the grep command will be very, very slow, and will affect other normal running programs.

Here, it is recommended to use tail command or head command.

Of course, the premise is to know the approximate location of the search content in the file, the head or the tail of the file.

Syntax

tail -f -c 1G file.log | grep "test"

head -c 100m file.log | grep "test"

tail:

head:

Examples

If we confirm that the content to be searched is at the end of the file, then we can use the following command to search for the specified content in the large file.

tail -f -c 1G file.log | grep "test"

If we confirm that the content to be searched is at the head of the file, then we can use the following command to search for the specified content in the large file.

head -c 1m  file.log | grep "test"

Of course, in some cases, in order to better understand the situation, we need to output the context information of the specified content. At this time, the following commands can be used.

head -c 1m  file.log | grep -C 20 "test"

grep:

Exit mobile version