How to suppress binary file matching results in grep
Last Updated :
02 Jan, 2023
In this article, we will discuss the topic of grep. Also, we will discuss what are the problems that occurred during binary files while using the grep command. Lastly, we will discuss how we can solve this problem using grep options.
Grep
The grep, Global Regular Expression Print is a command line utility that is used to print the string after matches. grep is one of the most useful commands in Unix/Linux systems. grep works as it copies a line into the buffer and then it compares with the search string. grep is made up of three commands egrep, grep, and fgrep that works in a similar way.
Problems with Binary Files using grep
Step 1: Binary Encoding
The problems occur when we try to print some specific word from a string but rather than printing that word it prints the binary file name. For example, we created a file with a UTF encoding error "\x80" and it displays binary file matches.
we have created a file encoding.txt using the echo command
echo 'Encoding\x80' >> encoding.txt
Print the specific word using grep
grep "Encoding" encoding.txt
Step 2: NULL Byte
A null byte is a byte that contains a value 0 which means 0x00 hex. when the grep command will be used then grep will treat as a binary file.
We have created a text file named null.txt that has a hex value of 0x00
echo "This file contains NULL\0x00" >> null.txt
When we try to grep that null file, it will show binary file matches
grep "NULL" null.txt
Grep with Binary Files
Step 3: Using the grep command without suppressing Binary Files
In Linux/Unix, there is a directory present called "/bin" that contains the user's binary file. so we will use the grep command without any options used.
grep "B" /bin/zstd
Grep without Binary Files
Step 4: Use the grep command with option -I for suppressing Binary Files
The grep command has a lot of options available for doing alternative tasks, to suppress the binary file, we have an option as -I or --binary-files=without-match
grep -I /bin/zstd
grep --binary-files=without-match "B" /bin/zstd
Step 5: Using the grep command with option -n
We can print the number of lines for searching that specific string. we can use option -n with the grep command.
cat /etc/smi.conf | grep -n "load"
Step 6: Use the grep command with option -H
When we have to print the file name along with the output then, grep has an option -H that can make use of it.
grep -H "path" /etc/smi.conf
Conclusion
In the above article, we have covered the definition of grep, the problems occurred when we use any binary file and finally, we have seen how we can take the option of grep (-I) for suppressing binary file matches using grep.
Similar Reads
How to Recursively Grep all Directories and Subdirectories in Linux
The grep command is one of the most frequently used utilities in Linux for searching through text files. It allows users to search for specific strings within files and directories, offering a wide range of options to customize the search process. In this article, weâll explore how to use the recurs
5 min read
How to compare two text files in python?
Python has provided the methods to manipulate files that too in a very concise manner. In this article we are going to discuss one of the applications of the Python's file handling features i.e. the comparison of files. Files in use: Text File 1Text File 2Method 1: Comparing complete file at once Py
3 min read
How to Use the grep Command in Linux with Examples?
Grep is a very powerful utility in Linux that is used for searching patterns within files or a stream of text. It's one of those essential tools that system administrators and developers use for parsing logs, cleaning up data, or otherwise dealing with large text apa. This tutorial will walk you thr
4 min read
How to Ignore a File in Git?
In this article, we are going to discuss how to ignore files in Git. Essentially, you can ignore files by specifying them in the ".gitignore" file. Moreover, you can also ignore directories using this method. Throughout this article, we'll cover everything from creating a GitHub account to setting u
3 min read
How To Show Only Filenames with grep on Linux
In Linux, the 'grep' command is one of the most used commands by many administrators to perform the advanced search of text within the files. Grep is one of the important text-search commands in Unix-based operating systems. We can also use this command to filter and simultaneously display the list
5 min read
How to Recursively Find all Files in Current and Subfolders Based on Wildcard Matching in Linux
Traversing through a directory tree to locate files is a common operation performed by most filesystem management software. This utility is in the form of command-line commands in most Operating Systems. In this article, you will learn how to find files using Wildcard Matching in Linux Operating Sys
5 min read
Reading binary files in Python
Reading binary files means reading data that is stored in a binary format, which is not human-readable. Unlike text files, which store data as readable characters, binary files store data as raw bytes. Binary files store data as a sequence of bytes. Each byte can represent a wide range of values, fr
5 min read
How to Find Out File Types in Linux
In Linux, everything is considered as a file. In UNIX, seven standard file types are regular, directory, symbolic link, FIFO special, block special, character special, and socket. In Linux/UNIX, we have to deal with different file types to manage them efficiently.Categories of Files in Linux/UNIXIn
7 min read
How to Address grep Error in R
When working with data, the grep function is often used for pattern matching in R Programming Language However, mistakes when using grep are fairly rare. These errors might range from simple syntax errors to more complicated issues with data and pattern validation. In this article, we will discuss c
3 min read
How to Search Git Repository By Commit Message?
Searching a Git repository by commit message can be incredibly useful for tracking changes, identifying specific updates, or understanding the history of a project. Git provides several ways to search for commit messages via the command line. Using Git Log with GrepThis approach involves using the g
1 min read