Questions tagged [grep]

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

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.

External references

Tutorials

Further reading

See also grep on Unix & Linux Stack Exchange for more detailed reading.

326 questions
19
votes
1 answer

Why bracket a single letter in a grep regex?

I've seen several instances where people are doing this: grep [f]oobar But I don't understand why that is preferable to grep foobar
hortitude
  • 565
  • 1
  • 3
  • 10
18
votes
2 answers

How do I show filenames only after a keyword grep search?

I currently have n data files in a directory where each file has at most 1 line of very long data. My directory structure is director/ data1.json data2.json data3.json I know that at least one of those files contain the keyword I'm…
Thierry Lam
  • 6,261
  • 10
  • 27
  • 24
18
votes
5 answers

Grepping through .gz log files

Does there exist a magical shell piping which would allow easily to grep through bunch of .gz log files without needing to extract them somewhere? .gz files are Apache logs, result of log rotation. I'd like to quickly check how often certain URIs…
Mikko Ohtamaa
  • 1,374
  • 3
  • 17
  • 28
17
votes
8 answers

Can awk be used instead?

I would like to get the number from rating as output from this # nc localhost 9571 language: language:en_ZA.UTF-8 language:en_ZW.UTF-8 session-with-name:Ubuntu Classic (No effects):gnome-session --session=2d-gnome session-with-name:Ubuntu (Safe…
Sandra
  • 10,303
  • 38
  • 112
  • 165
16
votes
1 answer

Ack search for literal strings

I am sick of having to escape things when I want to search for part of an html tag. How can I ack search for exactly what I type without having to escape stuff? e.g. ack-grep 'console.log(foo' I get: Unmatched ( in regex; marked by <-- HERE in…
tester
  • 565
  • 8
  • 18
14
votes
3 answers

How to programatically add secure_path in sudoers file

I'm trying to build a user data script for an EC2 instance that builds node and npm from github, and then starts a service. To grease these wheels, I need to add: :/usr/local/bin to the end of the line in /etc/sudoers which starts: Defaults …
belial
  • 263
  • 2
  • 7
14
votes
7 answers

How to Find a file in FreeBSD

I'm trying to locate all copies of example.filename on my FreeBSD server. What's the best / easiest / most efficient way to do this?
Ian
  • 1,498
  • 4
  • 26
  • 32
13
votes
3 answers

grep behaving differently on Fedora vs Ubuntu

So when I run this in Fedora I'm seeing this: $ ls hmm_data/indivA12_AATAAG/refs/par1/ 2R-orths.alleles 2R-ref.alleles $ ls hmm_data/indivA12_AATAAG/refs/par1/ | grep -F '-ref.alleles' 2R-ref.alleles But when I run on Ubuntu (same data) I don't…
Greg_the_Ant
  • 489
  • 7
  • 26
13
votes
2 answers

Grepping for CIDR ranges

From time to time I want to grep CIDR ranges out of my Apache log files. This is easy for ranges that fall on the natural boundaries (/8, /16 and /24) but not so easy for other ranges such as /17 and /25. Examples: # 192.168.0.0/16: (easy) grep "…
Ladadadada
  • 26,337
  • 7
  • 59
  • 90
13
votes
5 answers

Fast extraction of a time range from syslog logfile?

I've got a logfile in the standard syslog format. It looks like this, except with hundreds of lines per second: Jan 11 07:48:46 blahblahblah... Jan 11 07:49:00 blahblahblah... Jan 11 07:50:13 blahblahblah... Jan 11 07:51:22 blahblahblah... Jan 11…
mike
  • 3,963
  • 11
  • 30
  • 27
12
votes
3 answers

How can i send /var/mail/root again?

Through some misconfiguration I've a lot of emails in /var/mail/root and /var/mail/www-data How can I "loop" over these files and send each mail another time?
Max
  • 465
  • 2
  • 6
  • 11
12
votes
2 answers

How to find files that don't contain a given search string

I have a command that finds all the PDF files that contain the string "Font" find /Users/me/PDFFiles/ -type f -name "*.pdf" -exec grep -H 'Font' '{}' ';' How can I change this command such that it does the inverse? Finds all PDF files that do not…
Slinky
  • 1,027
  • 3
  • 15
  • 26
12
votes
8 answers

How do i get the default gateway in LINUX given the destination?

I'm trying to get the default gateway, using the destination 0.0.0.0 i used this command: netstat -rn | grep 0.0.0.0 and it returns this list: Destination - Gateway - Genmask - Flags - MSS - Window - irtt - Iface 10.9.9.17 - 0.0.0.0 …
Suezy
  • 223
  • 1
  • 2
  • 5
11
votes
6 answers

Grep progress bar using pv (pipe viewer)

I've got a huge directory on my computer and I need to search in every ruby file inside for a string. I could have done it like this : grep -R "string" *.rb but it takes really long and I'd like to use pv (pipe viewer) to show a progress bar to be…
Cydonia7
  • 233
  • 1
  • 2
  • 6
9
votes
2 answers

Cat, Grep, Redirect Output.... Blank File?

I just ran cat /opt/webapplications/Word/readme.log | grep -v 'Apple' and I got the output on the cli that I was expecting, which was all the lines in readme.log that did not contain 'Apple'... Next I ran... cat /opt/webapplications/Word/readme.log…
1
2
3
21 22