grep is a command-line tool for searching text patterns in files

learn more… | top users | synonyms

6
votes
2answers
4k views

Where does grep -r search by default?

I found if I search using grep without specifying a path, like grep -r 'mytext' it takes infinitely long. Meanwhile if I search with path specified grep -r 'mytext' . it instantly finds what I need. ...
6
votes
6answers
15k views

How to search for a word in entire content of a directory in linux

need to search for something in entire content I am trying: find . | xargs grep word I get error: xargs: unterminated quote How to achieve this?
6
votes
2answers
5k views

How to return both file name and line number with find … -exec grep?

When using find, how do I return the file name and the line number when searching for a string? I manage to return the file name in one command and the line numbers with another one, but I can't seem ...
6
votes
4answers
4k views

Finding the ID of a process and killing it

When ever I need to kill a background process I do ps -e | grep <process_name> Which prints something like this 1766 ? 00:00:13 conky , Then I use the process ID to kill it like so kill 1766 . ...
6
votes
1answer
5k views

How to grep lines which does not begin with “#” or “;”?

I want to grep smb.conf and see only lines which are not commented.
6
votes
3answers
10k views

Pipe find into grep -v

I'm trying to find all files that are of a certain type and do not contain a certain string. I am trying to go about it by piping find to grep -v example: find -type f -name '*.java' | xargs grep -v ...
6
votes
2answers
3k views

grep surrounding characters of a match

I'm looking to do a find and replace within a giant database dump, and it's not doing what I think should happen. I'd like to grep for my target string in the file, and then see the surrounding 8 ...
6
votes
6answers
436 views

Check if a variable contains only what I want, and nothing else

I'm writing a script to build a software from sources, and there's a --platforms option. I would like to allow the user to select multiple items, but I don't know how to prevent them from making a ...
6
votes
3answers
462 views

With grep, how can I match a pattern and invert match another pattern?

With grep, I want to select all lines that match a pattern, and that don't match another pattern. I want to be able to use a single invocation of grep so that I can use the --after-context option (or ...
6
votes
1answer
570 views

How to detect and delete lines containing ˆ@

I have a simple problem: In my file, the are lines containing the string ˆ@ˆ@ˆ@ˆ@ˆ@ˆ@. I just want to delete all lines with this string, using for example the sed or grep commands. And I would like ...
6
votes
3answers
8k views

How can I find all files that do NOT contain a text string?

What concise command can I use to find all files that do NOT contain a text string? I tried this (using -v to invert grep's parameters) with no luck: find . -exec grep -v -l shared.php {} \; ...
6
votes
1answer
428 views

find words with all vowels in order from a file

I have a large dictionary file with 300,000+ words in it and I'm trying to find all words with the vowels aeiou in that order and have only exactly 5 vowels. My current attempt does not seem to be ...
6
votes
4answers
4k views

How to find a file in Linux with its path?

I am trying to find settings.xml file in my Ubuntu machine. I have no clue where it is, and which directory it is in. I tried using this - ls -R | grep settings.xml But it doesn't show me the ...
6
votes
6answers
5k views

bash find lines starting with string

I have a bunch of files and I want to find which one contains sequential lines starting with a certain string. For example for the following file : Aaaaaaaaaaaa Baaaaaaaaaaa Cxxxxxxxxx Cyyyyyyyyy ...
6
votes
2answers
213 views

Why two almost idetical grep commands return different output: w/o and with filename

I have 2 almost identical greps: [Alex@localhost tmp]$ grep /bin/bash /etc/passwd root:x:0:0:root:/root:/bin/bash AlexL:x:500:500::/home/AlexL:/bin/bash user1:x:501:501:user1 ...
6
votes
3answers
110 views

generate links of pages with incrementing variable

consider the links http://unix.stackexchange.com/questions/tagged/linux?page=2&sort=newest&pagesize=15 ...
6
votes
3answers
240 views

cascaded grep matches color code as pattern

I am piping output of one grep comand into another grep. The first grep is using --color=always, so that the first match is colored. In practice, that means that the match is enclosed between two ...
6
votes
3answers
287 views

Print all lines not containing punctuation

I want a regex pattern which can print all lines not containing punctuation : Input : .I am line 1 I am ! line 2 I am line (3) I am line 4 Output: ( should be) I am line 4 What I've tried so ...
6
votes
2answers
654 views

What's the most efficient way to grep for two completely separate things and assign the values to separate variables?

CentOS 6.x I want to take the output from curl, grep for two completely separate strings, and assign their respective values as variables. What is the most efficient way to do this (without writing ...
6
votes
3answers
2k views

Grep_color on mac

I am trying to get my regex highlighted on mac using terminal. I have put this is my .profile: export GREP_COLOR='1;30;40' Nothing changes, so I added the same to my bash.profile still nothing, ...
6
votes
1answer
6k views

grep for words of no more than a certain length

I'm looking for a way to grep things like: i log for E M, i 1 f x i 0, I xi 1, 3 1,. simply based on number of characters. Nothing in that hypothetical output is longer than three characters. This ...
6
votes
1answer
2k views

grep: Ignoring GREP_OPTIONS to search case-sensitive

I have set GREP_OPTIONS="--ignore-case --color" in ~/.bashrc as I normally want grep to work case-insensitive. However, there are times when I need grep to actually search case-sensitive, but the man ...
6
votes
3answers
910 views

Print a line only if the next line does NOT contain a particular match

I am trying to search a log file for logged activities that did not complete. For example, I log a "Starting activity for ID 1234..." and if successful, the next line will be "Activity 1234 ...
6
votes
5answers
1k views

How can I recursively search directories for a pattern and just print out file names and line numbers

Currently I am using grep as follows: grep -lr <PATTERN> . This gives me the following output, containing a list of all files whose contents (not name) contain : ./path/to/file1 ...
6
votes
1answer
753 views

grep --color adds ANSI code ESC[K - This can change displayed text

Why does grep add ANSI code \e[K to its colored output? I can't see the point of it, but obviously the developers can. It is an ANSI/VT100 Terminal code which is used to "Clear line from current ...
6
votes
2answers
402 views

fastest way to grep jar file for a particular name in it?

I am trying to find all the jars which has spring in its name. I am working with windows and using cygwin to run the linux commands. Does my below command looks right? find . -name "*.jar" -exec jar ...
6
votes
3answers
2k views

how to execute lines coming from a grep result?

I've got a text file with certain installation instructions, and I know I can grep for a unique occurrence in the file. For example, the text file has something like: MYDIR=`find /home/user -name ...
6
votes
1answer
588 views

Peculiar piping grep/head behavior

I'm helping the netadmin here with a perl regex to automate operating on some snapshots from our SAN and our scripts does stuff like this: varinit1=$(iscsiadm -m session | grep rbmsdata1 | head -n1 | ...
5
votes
7answers
667 views

How to find a position of a character using grep?

I need to identify the postion of a character in string using grep command. Example, the string is RAMSITALSKHMAN|1223333. grep -n '[^a-zA-Z0-9\$\~\%\#\^]' How do I find the position of | in the ...
5
votes
3answers
3k views

How to grep for text in a file and display the paragraph that has the text?

Below is the text in the file: Pseudo name=Apple Code=42B state=fault Pseudo name=Prance Code=43B state=good I need to grep for "42B" and get the output from the above text like: Pseudo ...
5
votes
4answers
20k views

List the files containing a particular word in their text

I would like to list the files recursively and uniquely that contain the given word. Example: Checking for word 'check', I normal do is a grep $ grep check * -R But as there are many occurrence ...
5
votes
2answers
28k views

How to run grep on a single column?

I want to grep the output of my ls -l command: -rw-r--r-- 1 root root 1866 Feb 14 07:47 rahmu.file -rw-r--r-- 1 rahmu user 95653 Feb 14 07:47 foo.file -rw-r--r-- 1 rahmu user ...
5
votes
5answers
358 views

Number of files not containing given string

I read the question about how to find out the number of files that contain a certain string. This is possible with grep -l "string" * | wc -l. Is it possible to invert this, finding the number of ...
5
votes
3answers
9k views

Recursive grep for words in a particular file type

I wanted a command line command to search all shell scripts in the filesystem for a particular word, so I asked around at work and got the following solutions: grep word `find / -name \*.sh ...
5
votes
5answers
18k views

Multiline pattern match using sed, awk or grep

Is it possible to do a multiline pattern match using sed, awk or grep? Take for example, I would like to get all the lines between { and } So it should be able to match 1. {} 2. {.....} 3. ...
5
votes
1answer
473 views

inexact text search

Is there any utility like grep or even uniq but for inexact search or I should write it myself? I mean it will look at 90% (number may vary) matching, or smth like that. For example I have file with ...
5
votes
4answers
5k views

Find a string only in a specific file inside subdirectories

Let's say I need to find the function GetTypes() in all C# source file (.cs) the directories/subdirectories. I used grep -rn GetTypes *.cs, but I got an error with grep: *.cs: No such file or ...
5
votes
2answers
262 views

How do you get fgrep to find the literal “--help”?

fgrep --help | fgrep "--help" returns just the whole fgrep --help, how do I return just the lines that have the literal "--help" in them? The quotes don't do anything, nor does \-\-help.
5
votes
5answers
3k views

Recursively search folder but exclude svn subfolder

I want to find all occurences of foo in a large file system maintained by SVN but I don't want to match anything contained in any .svn directory. I get immediate matches when I do grep -l -e 'foo' * ...
5
votes
4answers
526 views

How to add a carriage return before every newline?

I have a file that only uses \n for new lines, but I need it to have \r\n for each new line. How can I do this? For example, I solved it in Vim using :%s/\n/\r\n/g, but I would like to use a script ...
5
votes
3answers
134 views

grep command giving error

I am using grep to filter out the contents on some patterns(genes in my case). For more info,here is the earlier link. Find pattern from one file listed in another My code(should work) but is not. ...
5
votes
3answers
394 views

difference between .* and * in regular Expression

I've a file named "test" that contains linux Unixlinux Linuxunix it's linux l...x now when i use grep '\<l.*x\>' , it matches : linux it's linux l...x but when i use grep '\<l*x\>' ...
5
votes
2answers
1k views

How to grep on source code without catching comments

I search a way to grep on source code without having sometimes false-positive because of comments. For instance if I search on foo on this .c source code : /* * foo has changed [...] and is now a ...
5
votes
1answer
630 views

Why do I have to escape a “dot” twice?

I know that we can escape a special character like *(){}$ with \ so as to be considered literals. For example \* or \$ But in case of . I have to do it twice, like \\. otherwise it is considered ...
5
votes
3answers
2k views

Recursive grep vs find / -type f -exec grep {} \; Which is more efficient/faster?

Which is more efficient for finding which files in an entire filesystem contain a string: recursive grep or find with grep in an exec statement? I assume find would be more efficient because you can ...
5
votes
3answers
2k views

Output to stdout and at the same time grep into a file

I have a script that outputs text to stdout. I want to see all this output in my terminal, and at the same time I want to filter some lines and save them in a file. Example: $ myscript Line A Line B ...
5
votes
4answers
220 views

How to replace m part of Lines with n part of Lines?

I have a file "test" like this: RewiteEngine On RewriteCond %{REMOTE_ADDR} !^192\.168\.1\.7$ [NC] RewriteCond %{REMOTE_ADDR} !^192\.168\.1\.8$ [NC] RewriteCond %{REMOTE_ADDR} ...
5
votes
1answer
182 views

grepping foo and bar

I am looking for a command options for grep to find files with the occurences of foo and bar. Grepping with grep -r -e foo -e bar . shows files which have only foo or only bar and files which ...
5
votes
5answers
7k views

Use grep to find all files in a directory with two strings [duplicate]

I am trying to figure out the correct syntax to find two strings, the entire part of each string, anywhere (doesn't have to be near each other) in a file. So any file that has both foo and say the ...
5
votes
2answers
3k views

What params do I pass to grep to return only file names?

I'm trying to use grep to find a specific piece of text in a bunch of files on my web server. No problem, except that it returns way more information than I want! Ideally it would just return a list ...