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

learn more… | top users | synonyms

8
votes
3answers
16k views

How do I count the number of occurrences of a word in a text file with the command line?

I have a large JSON file that is on one line, and I want to use the command line to be able to count the number of occurrences of a word in the file. How can I do that?
6
votes
2answers
2k views

How to make vertical bar | work in grep?

Here's an example of a grep command line I've tried: grep '(565172|565173|565175)' /var/log/cups/error_log It doesn't produce any output, even though all 3 values are in the file multiple times. I ...
7
votes
2answers
979 views

grep - why do brackets in grep pattern remove the grep process from ps results?

Why do brackets in a grep pattern remove the grep process from ps results? $ ps -ef | grep XXXX [...] XXXX [...] grep XXXX $ ps -ef | grep [X]XXX [...] XXXX
1
vote
3answers
504 views

Help with a find command

I have a bunch of directories. Inside these directories is a cover letter and a zip archive of work. I wanted to run a quick and easy query to open up all the cover letters so I can decide if it's ...
8
votes
4answers
8k views

grep — removing text after delimiter token

I have a file in which I need to eliminate everything after the first ; on every line. So a file like this: sdfsdsdf; fsdfsddf;sdfsd; Will result in this: sdfsdsdf fsdfsddf I have looked into ...
3
votes
1answer
260 views

Strings enclosed in '' are ignored when using grep

When I tried searching for a string within a file, the results didn't include those that were enclosed in single quotes. For example: grep -rn text folder/ The results didn't include strings the ...
41
votes
4answers
7k views

Convince grep to output all lines, not just those with matches

Say I have the following file: $ cat test test line 1 test line 2 line without the search word another line without it test line 3 with two test words test line 4 By default, grep returns each ...
14
votes
8answers
8k views

pgrep and pkill alternatives on mac os x?

Are there alternatives to pgrep and pkill commands on Mac OS X or should I just create aliases for them using other commands available for me?
17
votes
6answers
878 views

How can I highlight matches using GNU grep?

I have seen on some Linux, that grep is configured to highlight the match, and print the matching file. How can I configure grep to highlight matches?
11
votes
3answers
3k views

bash find xargs grep only single occurence

Maybe it's a bit strange - and maybe there are other tools to do this but, well.. I am using the following classic bash command to find all files which contain some string: find . -type f | xargs ...