EzDevInfo.com

grep interview questions

Top grep frequently asked interview questions

How to 'grep' a continuous stream?

Is that possible to use grep on a continuous stream?

What I mean is sort of a tail -f <file> command, but with grep on the output in order to keep only the lines that interest me.

I've tried tail -f <file> | grep pattern but it seems that grep can only be executed once tail finishes, that is to say never.


Source: (StackOverflow)

Grep, but only certain file extensions

I am working on writing some scripts to Grep certain directories, but these directories contain all sorts of file types.

I want to grep just .h and .cpp for now, but maybe a few others in the future.

So far I have:

{ grep -r -i CP_Image ~/path1/;

grep -r -i CP_Image ~/path2/;

grep -r -i CP_Image ~/path3/;

grep -r -i CP_Image ~/path4/;

grep -r -i CP_Image ~/path5/;} 

| mailx -s GREP email@domain.com

Can anyone show me how I would now add just specific file extensions?


Source: (StackOverflow)

Advertisements

Grep a tab in UNIX

How do I grep tab (\t) in files on the Unix platform?


Source: (StackOverflow)

How to grep (search) committed code in the git history?

I have deleted a file or some code in a file sometime in the past. Can I grep in the content (not in the commit messages)?

A very poor solution is to grep the log:

git log -p | grep <pattern>

However this doesn't return the commit hash straight away. I played around with git grep to no avail.


Source: (StackOverflow)

How do I grep for all non-ASCII characters in UNIX

I have several very large XML files and I'm trying to find the lines that contain non-ASCII characters. I've tried the following:

grep -e "[\x{00FF}-\x{FFFF}]" file.xml

But this returns every line in the file, regardless of whether the line contains a character in the range specified.

Do I have the syntax wrong or am I doing something else wrong? I've also tried:

egrep "[\x{00FF}-\x{FFFF}]" file.xml 

(with both single and double quotes surrounding the pattern).


Source: (StackOverflow)

What are good grep tools for Windows? [closed]

Any recommendations on grep tools for Windows? Ideally ones that could leverage 64-bit OS.

I'm aware of Cygwin, of course, and have also found PowerGREP, but I'm wondering if there are any hidden gems out there?


Source: (StackOverflow)

Windows recursive grep command-line

I need to do a recursive grep in Windows, something like this in Unix/Linux:

grep -i 'string' `find . -print`

or the more-preferred method:

find . -print | xargs grep -i 'string'

I'm stuck with just cmd.exe, so I only have Windows built-in commands. I can't install Cygwin, or any 3rd party tools like UnxUtils on this server unfortunately. I'm not even sure I can install PowerShell. Any suggestions using only cmd.exe built-ins (Windows 2003 Server)?


Source: (StackOverflow)

How to pipe stderr, and not stdout?

I have a program that writes information to stdout and stderr, and I need to grep through what's coming to stderr, while disregarding stdout.

I can of course do it in 2 steps:

command > /dev/null 2> temp.file
grep 'something' temp.file

but I would prefer to be able to do tis without temp files. Any smart piping trick?


Source: (StackOverflow)

How can I use grep to show just filenames (no in-line matches) on linux?

How can I use grep to show just file-names (no in-line matches) on Linux?

I am usually using something like:

find . -iname "*php" -exec grep -H myString {} \;

How can I just get the file-names (with paths) but without the matches? Do I have to use xargs? Didn't see a way to do this on my grep man page.


Source: (StackOverflow)

How to search contents of multiple pdf files?

How could I search the contents of PDF files in a directory/subdirectory? I am looking for some command line tools. It seems that grep can't search PDF files.


Source: (StackOverflow)

Exclude .svn directories from grep

When I grep my Subversion working copy directory, the results include a lot of files from the .svn directories. Is it possible to recursively grep a directory, but exclude all results from .svn directories?


Source: (StackOverflow)

Negative matching using grep (match lines that do not contain foo)

I have been trying to work out the syntax for this command:

grep ! error_log | find /home/foo/public_html/ -mmin -60

or

grep '[^error_log]' | find /home/baumerf/public_html/ -mmin -60

I need to see all files that have been modified except for those named error_log.

I've read about it here, but only found one not-regex pattern.


Source: (StackOverflow)

How to grep Git commit diffs or contents for a certain word?

In a Git code repository I want to list all commits that contain a certain word. I tried this

git log -p | grep --context=4 "word"

but it does not necessarily give me back the filename (unless it's less that 5 lines away from the word I searched for. I also tried

git grep "word"

but it gives me only present files and not the history.

How do I search the entire history so I can follow changes on a particular word? I mean to search my codebase for occurrences of word to track down changes (search in files history).


Source: (StackOverflow)

Grep a file, but show several surrounding lines?

I would like to grep for a string, but also show the preceding five lines and the following five lines as well as the matched line. I'm scanning for errors in a logfile, and want to see the context.

Is it possible?


Source: (StackOverflow)

Finding all files containing a text string on Linux

I'm trying to find a way to scan my entire Linux system for all files containing a specific string of text. Just to clarify, I'm looking for text within the file, not in the file name.

When I was looking up how to do this, I came across this solution twice:

find / -type f -exec grep -H 'text-to-find-here' {} \;

However, it doesn't work. It seems to display every single file in the system.

Is this close to the proper way to do it? If not, how should I? This ability to find text strings in files would be extraordinary useful for me for some programming projects I'm doing.


Source: (StackOverflow)