
- Kali Linux Tutorial
- Kali Linux - Home
- Installation & Configuration
- Information Gathering Tools
- Vulnerability Analyses Tools
- Kali Linux - Wireless Attacks
- Website Penetration Testing
- Kali Linux - Exploitation Tools
- Kali Linux - Forensics Tools
- Kali Linux - Social Engineering
- Kali Linux - Stressing Tools
- Kali Linux - Sniffing & Spoofing
- Kali Linux - Password Cracking Tools
- Kali Linux - Maintaining Access
- Kali Linux - Reverse Engineering
- Kali Linux - Reporting Tools
- Kali Linux Useful Resources
- Kali Linux - Quick Guide
- Kali Linux - Useful Resources
- Kali Linux - Discussion
How to grep multiline search patterns in Linux?
In order to be able to grep multiple lines of pattern present in a file, we will make use of the number of grep command that Linux provides us with. But first, we must understand what a grep command is and how to use it on Linux.
The grep command in Linux is used to filter searches in a file for a particular pattern of characters. It is one of the most used Linux utility commands to display the lines that contain the pattern that we are trying to search.
Normally, the pattern that we are trying to search in the file is referred to as the regular expression.
Syntax
grep [options] pattern [files]
While there are plenty of different options available to us, some of the most used are −
-c : It lists only a count of the lines that match a pattern -h : displays the matched lines only. -i : Ignores, case for matching -l : prints filenames only -n : Display the matched lines and their line numbers. -v : It prints out all the lines that do not match the pattern
Now, let’s consider a case where we want to find a particular pattern in all the files in a particular directory, say dir1.
Syntax
grep -rni "word" *
In the above command replace the “word” placeholder with
For that we make use of the command shown below −
Command
grep -rni "func main()" *
The above command will try to find a string “func main()” in all the files in a particular directory and also in the subdirectories as well.
Output
main.go:120:func main() {}
In case we only want to find a particular pattern in a single directory and not the subdirectories then we need to use the command shown below −
grep -s "func main()" *
Now suppose we have a text file named sample.txt whose content looks something like this.
$ immukul@192 linux-questions-code % cat sample.txt blabla blabla foofoo here is the text2strike Just a pattern bar blabla blabla
Now the command to search for multiline pattern is shown below
grep -Pzo "(?s)^(\s*)\N*/foo/,/bar/p.*?{.*?^\1}" .txt
Output
foofoo here is the text2strike just a pattern bar
- Related Articles
- Exclude Multiple Patterns With Grep on Linux
- How to preserve colouring after piping grep to grep in Linux?
- Search Within Specific File Types Using grep on Linux
- How to grep string without filenames in Linux?
- How to invert a grep expression on Linux?
- How to grep and replace a word in a file on Linux?
- How to limit the number of results returned from grep in Linux?
- How to suppress a binary file matching results in grep on Linux?
- How to grep a string in a directory and all its subdirectories in Linux?
- Exclude grep From ps Results on Linux
- How to match two strings that are present in one line with grep in Linux?
- How to use the grep command to search for a string that has a dot in it?
- How to Search and Remove Directories Recursively on Linux?
- How to define multiline String Literal in C#?
- How to make Ellipsis to multiline text in CSS?
