grep is a command-line tool for searching text patterns in files
45
votes
6answers
7k views
Is there a basic tutorial for grep, awk and sed?
I've been a Linux user for a while, and I've a pretty decent understanding of most the common command line utilities. However, ones that come up and up again in relation to programming are grep, awk, ...
44
votes
8answers
2k views
How can I prevent 'grep' from showing up in ps results?
When I search for some process that doesn't exist, e.g.
$ ps aux | grep fnord
wayne 15745 0.0 0.0 13580 928 pts/6 S+ 03:58 0:00 grep fnord
Obviously I ...
26
votes
4answers
7k views
What is the difference between `grep`, `egrep`, and `fgrep`?
Can any one tell me the technical difference between grep and egrep, and fgrep and provide a suitable example?
When do I need to use grep over egrep and vice versa?
25
votes
4answers
3k 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 ...
22
votes
2answers
2k views
Does grep use a cache to speed up the searches?
I have noticed that subsequent runs of grep on the same query (and also a different query, but on the same file) are much faster than the first run (the effect is easily noticeable when searching ...
18
votes
6answers
2k views
Is there a way to modify a file in-place?
I have a fairly large file (35Gb), and I would like to filter this file in situ (i.e. I don't have enough disk space for another file), specifically I want to grep and ignore some patterns — is there ...
17
votes
7answers
11k views
How to grep standard error stream (stderr)?
I am using ffmpeg to get the meta info of an audio clip. But I am unable to grep it.
$ ffmpeg -i 01-Daemon.mp3 |grep -i Duration
FFmpeg version SVN-r15261, Copyright (c) 2000-2008 Fabrice ...
14
votes
3answers
866 views
Piping from grep to awk not working
I am trying to grep the ongoing tail of file log and get the nth word from a line. Example file:
$ cat > test.txt <<EOL
Beam goes blah
John goes hey
Beam goes what?
John goes forget it
Beam ...
14
votes
7answers
591 views
Pimp my GNU grep
I have seen on some Linux, grep is configured to highlight the match, and print the matching file.
How do you configure GNU grep best?
14
votes
2answers
690 views
What is a “loop device” when mounting?
I am doing a mounting of an iso file and looking at this tutorial. They use the command:
$ mount -o loop disk1.iso /mnt/disk
I'm trying to understand the use of -o loop. So I have two questions:
...
13
votes
5answers
14k views
grep: count total number of occurrences
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 ...
13
votes
3answers
8k views
How to search text throughout entire file system?
Assuming that the grep tool should be used, I'd like to search for the text string "800x600" throughout the entire file system.
I tried:
grep -r 800x600 /
but it doesn't work.
What I believe my ...
12
votes
3answers
4k views
Can grep output only specified groupings that match?
Say I have a file:
# file: 'test.txt'
foobar bash 1
bash
foobar happy
foobar
I only want to know what words appear after "foobar", so I can use this regex:
"foobar \(\w\+\)"
The parenthesis ...
12
votes
2answers
2k views
How do I pass a list of files to grep
I am using find and getting a list of files I want to grep through. How do I pipe that list to grep?
11
votes
11answers
5k views
Grep huge number of patterns from huge file
I have a file that's growing about 200,000 lines a day, and it is all formed with blocks of three lines as such:
1358726575123 # key
Joseph Brunner # name
carpenter # job
...
11
votes
6answers
359 views
How can I grep a directory based on the contents of two successive lines?
How can I grep a directory for lines that contain "Foo", but only get matches when the next line also contains "Bar"?
11
votes
3answers
370 views
How can I redirect matching lines to a file, and non-matching lines to a different file?
Is there a script/program/utility already available for the following requirement in a optimised way?
someCommand |
tee >(grep "pattern" > LinesWhichMatch) |
grep -v "pattern" > ...
11
votes
3answers
832 views
Online grep exercises?
Are there any online grep tutorials that would feature some online tool for performing exercises? For example, having some predefined text sample and being told what patterns to find and so forth? It ...
10
votes
7answers
6k views
grep and tail -f?
Is it possible to do a tail -f (or similar) on a file, and grep it at the same time? I wouldn't mind other commands just looking for that kind of behavior.
10
votes
6answers
322 views
How to find files with 100% NUL characters in their contents?
What is the Linux command-line command that can identify such files?
AFAIK the find command (or grep) can only match a specific string inside the text file. But I want to match whole contents, i.e. ...
10
votes
2answers
1k 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 ...
9
votes
7answers
6k views
grep pdf files?
Is there a way to search pdf files using the power of grep, without converting to text first in Ubuntu?
9
votes
4answers
2k views
Grep: Show all the file up to the match
grep --before-context 5 shows 5 lines before the match.
I want to show everything before the match.
Doing grep --before-context 99999999 would work but it is not very... professional.
How to show ...
9
votes
2answers
2k views
How to remove multiple lines per occurrence in a file?
Say I have this 857835 line file, containing stuff like this:
a1
rubbish1
rubbish2
rubbish3
rubbish4
a1
rubbish5
rubbish6
rubbish7
rubbish8
And I wish to remove all occurences of a1 and the next ...
9
votes
2answers
542 views
How to run grep with multiple AND patterns?
I would like to get the multi pattern match with implicit AND between patterns, i.e. equivalent to running several greps in a sequence:
grep pattern1 | grep pattern2 | ...
So how to convert it to ...
9
votes
6answers
5k 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?
9
votes
2answers
179 views
“Grep breaks, causing the boot process to hang forever”
Question within a question (from the Debian wiki):
Should I use sid on my desktop?
If you think you can handle a broken Debian system, sure. Do you know
what to do if libpam0g breaks, ...
9
votes
2answers
253 views
grep: Find all lines that contain Japanese kanjis
In a huge UTF-8 text file, I want to show all lines that contains Japanese kanjis.
What grep (or other) expression does this?
If I am not mistaken, kanjis are the characters between \u4e00 and ...
9
votes
1answer
247 views
Grep alias - line numbers unless it's in a pipeline
I want to create a bash alias for grep that adds line numbers:
alias grep='grep -n'
But that, of course, adds line numbers to pipelines as well. Most of the time (and no exceptions come to mind) I ...
8
votes
6answers
1k views
How do I list every file in a directory except those with specified extensions?
Suppose that I have a folder containing .txt, .pdf, and other files. I would like to list the "other" files (i.e., files not having the extensions .txt or .pdf). Do you have any advice on how to do ...
8
votes
3answers
871 views
Is there a 'rc' configuration file for grep/egrep? (~/.egreprc?)
I usually do some grep when developing, and there are some extensions that I always don't want to look for (like *.pyc).
Is it possible to create a ~/.egreprc or something like that, and put some ...
8
votes
5answers
2k views
Adding numbers from the result of a grep
I run the following command:
grep -o "[0-9] errors" verification_report_3.txt | awk '{print $1}'
and I get the following result:
1
4
0
8
I'd like to add each of the numbers up to a running count ...
8
votes
5answers
548 views
How intelligent are unix utilities?
Unix utilities like sort, find, grep, diff et al are very handy to perform quick tasks, sometimes without writing any code at all.
I wanted to know what algorithms do they internally use and how do ...
7
votes
8answers
3k views
How can I “cat” a file and remove commented lines?
I'd like to know if there is a way that I could cat file like php.ini and remove all lines starting with ;
For example, if the file contained this:
; - Show all errors, except for notices
;
...
7
votes
2answers
205 views
“grep -r foo *” vs “grep -r foo .”
When using grep -r you could search in all files with either * or .
and it seems to return the same thing but is it really the same?
Let's say I search for "foo", then I could write
grep -r foo *
...
7
votes
6answers
1k views
Is there any alternative to grep's -A -B -C switches (to print few lines before and after )?
grep -A 2 -B 3
prints 2 lines after the grep string and prints 3 lines before.
grep -C 3
prints 3 Lines before and 3 lines after
Unfortunately, the grep I'm using does not support these ...
7
votes
4answers
13k views
How do I grep for multiple patterns?
I want to find all lines in several files that match one of two patterns. I tried to find the patterns I'm looking for by typing
grep (foo|bar) *.txt
but the shell interprets the | as a pipe and ...
7
votes
4answers
4k 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 ...
7
votes
3answers
1k views
How to perform GREP on FTP?
I tried searching for similar command in ncftp (actually this is the only terminal ftp client I used) and I didn't find it.
What's the way (if possible at all) to use grep command on remote server, ...
7
votes
2answers
354 views
How do I take a list and remove it from a file?
I have a long list of domain names that I need to remove from /etc/remotedomains. They're probably not in any particular order in the file. Each domain is on one line.
How could I iterate through the ...
6
votes
1answer
6k views
Bash: assign ls | grep to a variable and echo it with a string
I want to assign the result of an expression to a variable and concatenate it with a string, then echo it. Here's what I've got:
#!/bin/bash
cd ~/Desktop;
thefile= ls -t -U | grep -m 1 "Screen Shot";
...
6
votes
2answers
381 views
When is it useful to use “grep — SOMEPATTERN”?
So under Linux/AIX where ever.., when is it useful to use:
grep -- SOMEPATTERN
the Question is the "--" part. When is it usefull to use?
6
votes
2answers
6k views
What does ^d mean in ls -l | grep ^d?
When I do ls -l | grep ^d it lists only directories in the current directory.
What I'd like to know is what does the caret ^ in ^d mean?
6
votes
3answers
8k 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
4answers
2k views
Number of files containing a given string
How can I count the number of files (in a directory) containing a given string as input in bash/sh?
6
votes
3answers
5k views
How can you move (or copy) all files of a certain type to a directory in Unix?
I'm not very familiar with all the tricks of grep/find/awk/xargs quite yet.
I have some files matching a particular pattern, say *.xxx. These files are in random places throughout a certain ...
6
votes
3answers
3k views
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 ...
6
votes
1answer
223 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
4answers
3k views
Finding a substring in files across subdirectories with a single built-in command?
In Windows, if I wanted to find a string across all files in all subdirectories, I would do something like
findstr /C:"the string" /S *.h
However, in Linux (say, Ubuntu) I have found no other way ...
6
votes
3answers
4k 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 ...
