
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to match two strings that are present in one line with grep in Linux?
In order to be able to grep two strings that exists on the same line in Linux command line, we must first 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 −
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()" *
In the above command we made use of the -s flag which will help us to not get a warning for each subdirectory that is present inside the directory where we are running the command.
Output
main.go:120:func main() {}
Now, consider that I have a .txt file and the content of the file looks something like this.
immukul@192 d2 % cat 2.txt orange apple is great together apple not great is apple good orange good apple not
Now I want to use the grep command for all the lines that contain both the words grep 'orange' 2.txt | grep 'apple'‘apple’ and ‘orange’.
Command
grep 'orange' 2.txt | grep 'apple'
Output
immukul@192 d2 % grep 'orange' 2.txt | grep 'apple' orange apple is great together orange good apple not
- Related Questions & Answers
- How to preserve colouring after piping grep to grep in Linux?
- How to grep string without filenames in Linux?
- How to grep multiline search patterns in Linux?
- How to write multiple line strings using Bash with variables on Linux?
- Count of strings that become equal to one of the two strings after one removal in C++
- How to list one filename per output line in Linux?
- How to invert a grep expression on Linux?
- Python Program that Displays which Letters are Present in Both the Strings
- How to compare two sorted files line by line in the Linux system?
- How to concatenate multiple C++ strings on one line?
- Python program to remove words that are common in two Strings
- Delete every one of the two strings that start with the same letter in JavaScript
- How to match strings that aren't entirely digits in JavaScript?
- Convert XLSX to CSV in Linux with Command Line in Linux
- How to separate strings in R that are joined with special characters?