- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 grep string without filenames in Linux?
We know that we can make use of the grep command to search for a particular pattern of characters in all the lines of a file or multiple files. The grep command performs a case-insensitive search for words in a file.
Let’s see a simple example where we will grep a pattern in all the files that are present in a directory.
grep -i ‘lp’ Sample*
Sample: The sample was printed via the internet. Sample: I believe lp cares about what the device is. Sample1: This was printed via the internet. Sample1: I believe lp cares about what the device is. Sample2: This was printed via the internet. Sample3: I believe lp cares about what the device is.
The only concern for this particular scenario is that we don’t want the filename to be printed along with the output. We simply want the string where a particular pattern is found and we just need to print that string.
In that case we can use the -h parameter which can be used to hide the filenames.
grep -i -hn ‘lp’ Sample*
According to the Linux Docs, the -h flag does the following,
Suppress the prefixing of file names on output. This is the default when there is only one file (or only standard input) to search.”
The sample was printed via the internet. I Believe lp cares about what the device is. This was printed via the internet. I believe lp cares about what the device is. This was printed via the internet. I believe lp doesn't care what the device is
- How to preserve colouring after piping grep to grep in Linux?
- How to grep multiline search patterns in Linux?
- How to invert a grep expression on Linux?
- How to grep a string in a directory and all its subdirectories in 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 match two strings that are present in one line with grep in Linux?
- Extracting filenames from a path in MySQL?
- How to use the grep command to search for a string that has a dot in it?
- Resetting a Root Password in Linux without External Media
- How to check the syntax of a Bash script without running it in Linux?
- How to perform grep operation on all files in a directory?
- How to swap two String variables without third variable.
- How to open multiple filenames in Tkinter and add the file names to a list?