Forrest logo
back to the zegrep tool

zegrep:tldr:3b3a6

zegrep: Search for lines matching a pattern, printing only the matched text.
$ zegrep --only-matching "${search_pattern}" ${filename}
try on your machine

The command zegrep --only-matching "${search_pattern}" ${filename} is used to search for a specific pattern in a given file using extended regular expressions (ERE) with zgrep.

Here's a breakdown of the command:

  • zegrep: This is the command itself. It is a variant of zgrep, a utility that searches compressed files for lines that match a specified pattern.

  • --only-matching: This flag is used to instruct zegrep to only display the parts (substrings) of the line that match the specified pattern. It filters out the entire line containing the match and only shows the matching substrings.

  • "${search_pattern}": This is a placeholder for the actual pattern you want to search for. It should be surrounded by double quotes (") to avoid any potential issues with special characters in the pattern.

  • ${filename}: This is the name of the file you want to search in. It can be a single file or multiple filenames separated by spaces.

When you run the zegrep command with the provided options and arguments, it scans the specified file(s) with the chosen pattern and returns the matching substrings on separate lines.

This explanation was created by an AI. In most cases those are correct. But please always be careful and never run a command you are not sure if it is safe.
back to the zegrep tool