How to Grep Multiples Lines and using Specific Keyword on Linux

In Linux, grep is a powerful command-line utility that is used to search and filter text-based data. Grep is used to find patterns in files and directories and can search through multiple files or directories simultaneously. One of the most commonly used options of the grep command is to search for a specific keyword within a file.

In this guide, we will explain how to use grep to search for multiple lines of text that contain a specific keyword.

Step 1:

Open a terminal window on your Linux machine and navigate to the directory containing the file(s) you want to search.

cd /path/to/directory

Step 2:

Use the following command to search for a specific keyword within multiple lines of text in a file:

grep -A [number_of_lines] 'keyword' filename

The “-A” option is used to print “number_of_lines” lines of text after the matched line. For example, if you want to print three lines after the matched line, use “-A3”. Replace “number_of_lines” with the number of lines you want to print after the matched line. Replace “keyword” with the word you want to search for, and “filename” with the name of the file you want to search in.

See also  How to Install 389 Directory Server on CentOS 5.8

Step 3:

If you want to search for the keyword within multiple files in a directory, use the following command:

grep -A [number_of_lines] 'keyword' *

The “*” wildcard character will match all files in the current directory. Replace “number_of_lines”, “keyword”, and “filename” with the appropriate values.

Step 4:

If you want to search for the keyword within a directory and its subdirectories, use the following command:

grep -r -A [number_of_lines] 'keyword' /path/to/directory

The “-r” option stands for recursive and will search through all subdirectories of the specified directory. Replace “number_of_lines”, “keyword”, and “/path/to/directory” with the appropriate values.

Examples

Assumed that you have exported all log for 29 March 2012 from /var/log/messages into 29032012.txt as below :

[root@centos62 ~]# more /var/log/messages | grep "Mar 29" > 29032012.txt

How to Grep Multiples Lines

Please grep “cubic” with -B1 and -A4 :

[root@centos62 ~]# grep -B1 -A4 "cubic" 29032012.txt

The output will return like this :

Mar 29 21:04:16 centos62 kernel: usbhid: v2.6:USB HID core driver
Mar 29 21:04:16 centos62 kernel: TCP cubic registered
Mar 29 21:04:16 centos62 kernel: Initializing XFRM netlink socket
Mar 29 21:04:16 centos62 kernel: NET: Registered protocol family 17
Mar 29 21:04:16 centos62 kernel: Using IPI No-Shortcut mode
Mar 29 21:04:16 centos62 kernel: registered taskstats version 1

How to Grep using Specific Keyword :
Grep only keyword “BIOS-e820” from 29032012.txt file as below :

[root@centos62 ~]# more 29032012.txt | grep "BIOS-e820"

The output will return like this :

Mar 29 21:04:16 centos62 kernel: BIOS-e820: 0000000000000000 - 000000000009f800 (usable)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 000000000009f800 - 00000000000a0000 (reserved)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 00000000000ca000 - 00000000000cc000 (reserved)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 00000000000dc000 - 00000000000e0000 (reserved)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 00000000000e4000 - 0000000000100000 (reserved)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 0000000000100000 - 000000003fef0000 (usable)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 000000003fef0000 - 000000003feff000 (ACPI data)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 000000003feff000 - 000000003ff00000 (ACPI NVS)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 000000003ff00000 - 0000000040000000 (usable)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 00000000e0000000 - 00000000f0000000 (reserved)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 00000000fec00000 - 00000000fec10000 (reserved)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 00000000fee00000 - 00000000fee01000 (reserved)
Mar 29 21:04:16 centos62 kernel: BIOS-e820: 00000000fffe0000 - 0000000100000000 (reserved)

Commands Mentioned:

  • grep – search and filter text-based data.
  • -A – print “number_of_lines” lines of text after the matched line.
  • * – wildcard character that matches all files in the current directory.
  • -r – recursive search through all subdirectories of the specified directory.
See also  How to Install Webmin 1.570 on CentOS 5.7 from tar.gz file

Conclusion

In this guide, we have explained how to use the grep command to search for multiple lines of text that contain a specific keyword in Linux. By following the steps outlined in this guide, you can quickly and easily filter through large amounts of text-based data to find the information you need. This is a useful skill for Linux users who work with text-based files and data regularly. If you have any comments or suggestions for improving this guide, please let us know in the comments below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *