Questions tagged [grep]

grep is a Unix command-line utility that searches input for lines matching (or not) a regular expression and prints the results.  Use this tag for questions about how to use grep features, how to write regular expressions that work with grep, and general usage.  Because grep is now available in Unix-like environments (e.g., Cygwin on Windows), you can also use this tag for compatibility questions, and differences between versions and software environments.

grep is a Unix command-line utility that searches input passed to it for lines (or NUL-terminated records) matching (or not matching) a regular expression (or regular expressions), and prints the results.  As is typical for Unix command-line utilities, it can take input from files and/or read the standard input.

Usage

Use this tag for questions about how to use grep features, how to write regular expressions that work with grep, and general usage.  Because grep is now available in Unix-like environments (including Cygwin on Windows), you can also use this tag for compatibility questions, and differences between versions and software environments.

Related tags

References

1114 questions
92
votes
9 answers

grep to find files that contain ^M (Windows carriage return)

I use Linux. There is a pesky ^M (Windows cariage return) somewhere hidden in thousands of configuration files, and I have to find it, because it makes the server fail. How do I find ^M among a directories hierarchy full of configuration files? I…
Nicolas Raoul
  • 11,071
23
votes
3 answers

How to grep a log file within a specific time period

I have a log file, each line in the log is prepend with a date, like so: 2012-03-06 11:34:48,657 blah blah blah... How do I grep this file and get only the lines from 8am to 11pm only? My intention is I want to count the number of errors happening…
Rosdi
  • 906
17
votes
3 answers

How to have grep print only the matching text plus X surrounding characters

I need something similar to grep -A and grep -B but for characters. In other words, I have a file with incredibly long lines, e.g.: [thousands of characters] mytext [thousands of characters] If I do grep mytext file, I don't want the full lines…
10
votes
4 answers

grep does not recurse for files with a specific extension

Whenever I use grep with gnuwin32's recurse option -r and include a glob pattern for files to search (e.g. *.c), no files in the subdirectories are searched. I am using the latest grep from gnuwin32. Specifically, I was searching for the string…
9
votes
3 answers

How can I grep a hex value in a string in a binary file?

I have a binary file like this (open in Emacs hex mode): How can I grep if hex values '22081b00081f091d2733170d123f3114' exists in the file? 00000000: 2b08 1b00 1418 0825 0407 3830 271d 170d +......%..80'... 00000010: 2208 1b00 081f 091d 2733 170d…
michael
  • 5,945
9
votes
2 answers

Use grep for a long line to get the part of the line

I want to search in a long sentence (more than 1024 letters). I have one text file (test.txt) which has one long sentence, like this: afdafglwqgkjrldjl;ewqje;'k;g;je;;;fdsgalsdkf;akslg;safdas.....dasfsd Now I want to check which line contains the…
whitebear
  • 705
  • 4
  • 13
  • 28
5
votes
2 answers

Exclude long lines from grep results

How do I exclude very long lines from grep results? I often grep through lots of .js files (-r) and some of them are compiled, so they consist of a single line usually a couple thousand characters long. From all that clutter I find it hard to see…
Attila O.
  • 1,377
5
votes
1 answer

grep simply fails when used on a few files

I've been trying for about the past 30 minutes to get this to work properly. grep is not exactly the most difficult thing to use, so I'm somewhat baffled as to why this won't work. The files I'm trying to use grep on are simple XHTML log files.…
Reid
  • 255
4
votes
1 answer

How to grep through files which match a regex

I wish to search for keyword "test", in all files which are of type "*.spec.in" in the current directory recursively This doesn't work: grep -rn "test" ./*.spec.in How do I do this?
Bruce
  • 3,137
4
votes
1 answer

Get additional data around match from grep

I'm wondering how to improve my grep use when being confronted with files with very long lines. Imagine something like "packed" JavaScript files that only have 1 line. Sometimes files like that spit out results which I didn't expect and flood my…
Oliver Salzburg
  • 87,539
  • 63
  • 263
  • 308
4
votes
2 answers

How to search file *contents* (grep/ag/rg) using FZF?

When I run a project-wide (multi-file) search through FZF, the results are flooded with lines whose filepath matches the query, crowding out the more relevant results where the file contents matched. For example, running the Ag example from the…
ivan
  • 1,055
3
votes
1 answer

How can I mark two columns in the middle of text

How can I mark two columns in the middle of text. grep --color '^[[:alnum:]]*[[:blank:]]' file This mark text in the beginning, but I want to mark second and third columns. How can pass first one?
diego9403
  • 907
3
votes
1 answer

How do I grep a string from files created within the last 5 hours?

I have a directory that is populated every 30 mins with a text file, each day I need to grep the line with "Quality data" on it but I only want to grep from the 10 most recent files. How can I tell grep to only look in files created within the last…
Mel
  • 31
  • 1
  • 2
3
votes
2 answers

How can I find a duplicate copy of an image file?

I have an image file, and I know there's another copy of it somewhere in a directory or one of its ancestors, but it has a different, unknown file name, and there are thousands of images in this directory. How can I find the file name of the image…
3
votes
2 answers

What does grep do when using a dollar sign in the search term?

I wanted to find $featuredicon variable in my php files, and I ran grep -ir "$featuredicon" I understood that dollar sign is reserved character in shell. But what does it actually do? Did I modify my php files? I'm afraid I did something bad... next…
user98645
1
2 3 4 5