Questions tagged [grep]

For questions pertaining to grep, a command-line tool for searching text patterns in files. Use this tag for questions about grep itself or questions about issues arising from using the grep command-line tool.

The grep utility searches a pattern (regular expression) in a text file. There are many options to control how matches are performed and how to display matches.

The name grep comes from the ed (a Unix line editor) command g/re/p which means “globally search for a regular expression and print all lines containing it”.

External references

Tutorials

Further reading

5025 questions
407
votes
7 answers

Count total number of occurrences using grep

grep -c is useful for finding how many times a string occurs in a file, but it only counts each occurence once per line. How to count multiple occurences per line? I'm looking for something more elegant than: perl -e '$_ = <>; print scalar ( () =…
user4518
257
votes
6 answers

Can grep return true/false or are there alternative methods

As a part of this script, I need to be able to check if the first argument given matches the first word of file. If it does, exit with an error message; if it doesn't, append the arguments to the file. I understand how to write the if statement, but…
Lauren
  • 2,635
242
votes
10 answers

What makes grep consider a file to be binary?

I have some database dumps from a Windows system on my box. They are text files. I'm using cygwin to grep through them. These appear to be plain text files; I open them with text editors such as notepad and wordpad and they look legible. However,…
user394
  • 14,404
  • 21
  • 67
  • 93
156
votes
5 answers

Grep: how to add an "OR" condition?

How can I introduce a conditional OR into grep? Something like, grepping a file's type for (JPEG OR JPG), and then sending only those files into the photos folder. For example. I know how to send the file where I want it, and get the file type, I…
Harv
  • 2,024
137
votes
9 answers

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

I want to grep smb.conf and see only lines which are not commented.
denys
  • 1,649
133
votes
5 answers

Match exact string using grep

I have a text file: deiauk 1611516 afsdf 765 minkra 18415151 asdsf 4152 linkra sfsfdsfs sdfss 4555 deiauk1 sdfsfdsfs 1561 51 deiauk2 115151 5454 4 deiauk 1611516 afsdf ddfgfgd luktol1 4545 4 9 luktol 1 and I want to match exactly deiauk. When I do…
user3334375
  • 1,795
118
votes
3 answers

Why doesn't grep ignore binary files by default?

The manpage for grep describes the -I flag as follows: -I Ignore binary files. This option is equivalent to --binary-file=without-match option. It also says this about binary files: --binary-files=value Controls searching and…
user36078
114
votes
8 answers

Detecting pattern at the end of a line with grep

If I do: $ ls -R .: 4Shared/ Cloud/ ./4Shared: UFAIZLV2R7.part3.rar ./Cloud: UFAIZLV2R7.part2.rar.part UFAIZLV2R7.part1.rar.part UFAIZLV2R7.part4.rar.part If I want to list the .rar files only, and I use grep, it will show me too the .rar.part…
98
votes
2 answers

Reading grep patterns from a file

I have a couple of big text files and in the file UNIQS.txt I have a list of strings to grep from another file. The code I use is grep -f UNIQS.txt EEP_VSL.uniqs.sam > UNIQ_templates.sam which does nothing - the file generated is empty. But when I…
75
votes
3 answers

How to show lines after each grep match until other specific match?

I know that by using the "-A NUM" switch I can print specific number of trailing lines after each match. I am just wondering if it's possible to print trailing lines until a specific word is found after each match. e.g. When I search for "Word A" I…
B Faley
  • 4,343
  • 11
  • 39
  • 48
66
votes
4 answers

What is the difference between grep -e and grep -E option?

I am trying to understand the difference between grep -e and grep -E. Now from grep manpage I got: -E, --extended-regexp Interpret PATTERN as an extended regular expression (see below). -e PATTERN, --regexp=PATTERN Use PATTERN as the pattern;…
ronnie
  • 901
56
votes
2 answers

I would like to grep all files except some file types?

How do I recursively grep files within a given folders except a couple file types? For example, I'm looking for a string within my workspace folder but it ends up searching inside sql files and generates serialized strings. So in this case, I'd…
chrisjlee
  • 8,523
52
votes
1 answer

grep search + next line

With the grep command, I found the text I need as follows: grep 'C02' ~/temp/log.txt Now, wherever I find the desired string, I would like to print the line following the found string. For example, let's say that the desired text is abc, and abc is…
Mah
  • 521
52
votes
6 answers

How to display lines 2-4 after each grep result?

I'm parsing a mailbox file that stores e-mail server reports for unsuccessfully delivered e-mail. I wish to extract bad e-mail addresses, so that I remove them from the system. The log file looks like this: ...some content... The…
49
votes
2 answers

Get `grep` to not output file name

When I use grep -o to search in multiple files, it outputs each result prefixed with the file name. How can I prevent this prefix? I want the results without the file names.
Ram Rachum
  • 1,825
1
2 3
31 32