10 grep command Interview Questions and Answers
Prepare for technical interviews with this guide on mastering the grep
command, featuring common questions and detailed answers.
Prepare for technical interviews with this guide on mastering the grep
command, featuring common questions and detailed answers.
The grep
command is a powerful and versatile tool used in Unix-based systems for searching and manipulating text. It allows users to search through files and directories for specific patterns, making it an essential utility for developers, system administrators, and data analysts. With its ability to handle regular expressions and perform complex searches, grep
is indispensable for efficient text processing and data extraction tasks.
This article provides a curated selection of interview questions focused on the grep
command, designed to help you demonstrate your proficiency and problem-solving skills. By familiarizing yourself with these questions and their answers, you will be better prepared to showcase your expertise in using grep
effectively during technical interviews.
grep
command to find all lines containing the word “error” in a file named log.txt
.The grep
command in Unix-based systems is used to search for specific patterns within files. To find all lines containing the word “error” in a file named log.txt
, use:
grep "error" log.txt
This command searches for “error” in log.txt
and prints matching lines. The search is case-sensitive by default. To make it case-insensitive, add the -i
option:
grep -i "error" log.txt
grep
command to make the search case-insensitive?To make the search case-insensitive, use the -i
option. This tells grep
to ignore case distinctions in both the pattern and the input files.
Example:
grep -i "pattern" filename
results.txt
?To count the number of lines containing a specific word, use the -c
option with grep
.
Example:
grep -c "success" results.txt
This command returns the number of lines in results.txt
that contain “success”.
grep
command to display line numbers along with the lines that contain the word “function” in a file named script.py
.To display line numbers with lines containing a specific word, use the -n
option with grep
.
Example:
grep -n "function" script.py
This command shows each matching line in script.py
along with its line number.
grep
to find all lines in a file named data.csv
that do not contain the word “NULL”?To find lines that do not contain a specific word, use the -v
option, which inverts the match.
Example:
grep -v "NULL" data.csv
This command prints all lines in data.csv
that do not contain “NULL”.
grep
command using a regular expression to find lines that start with a digit in a file named numbers.txt
.To find lines that start with a digit in numbers.txt
, use a regular expression. The caret symbol (^) denotes the start of a line, and [0-9]
matches any digit.
Example:
grep '^[0-9]' numbers.txt
grep
command to match whole words only, avoiding partial matches?To match whole words only and avoid partial matches, use the -w
option.
Example:
grep -w "word" filename.txt
This command searches for the exact word “word” in filename.txt
.
grep
command to display 3 lines before and after each match of the word “error” in a file named log.txt
.To display 3 lines before and after each match of “error” in log.txt
, use the -B
and -A
options.
Example:
grep -B 3 -A 3 "error" log.txt
In this command, -B 3
includes 3 lines before each match, and -A 3
includes 3 lines after.
grep
command to invert the match results, showing lines that do not contain the word “debug” in a file named output.log
.To invert match results, showing lines that do not contain a specific word, use the -v
option.
Example:
grep -v "debug" output.log
This command displays all lines in output.log
that do not contain “debug”.
grep
command to find lines containing the word “error” but exclude lines that contain the word “warning” in a file named log.txt
.To find lines containing “error” but exclude those with “warning” in log.txt
, use a combination of grep
commands piped together.
Example:
grep "error" log.txt | grep -v "warning"
This command first searches for “error” and then excludes lines containing “warning”.