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

learn more… | top users | synonyms

26
votes
7answers
5k 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 ...
1
vote
3answers
333 views

Switch from grep to sed

I have a files students.txt, with lines of the form: Surname, Forename: Day.Month.Year: Degree For example: Smith, John: 15.01.1986: MSc IT Taylor, Susan: 04.05.1987: MSc IT Thomas, Steve: ...
5
votes
5answers
18k views

Multiline pattern match using sed, awk or grep

Is it possible to do a multiline pattern match using sed, awk or grep? Take for example, I would like to get all the lines between { and } So it should be able to match 1. {} 2. {.....} 3. ...
5
votes
2answers
3k views

What params do I pass to grep to return only file names?

I'm trying to use grep to find a specific piece of text in a bunch of files on my web server. No problem, except that it returns way more information than I want! Ideally it would just return a list ...
6
votes
3answers
9k views

What does \? mean in a regular expression?

The following command is used to search for a 7-digit phone number: grep "[[:digit:]]\{3\}[ -]\?[[:digit:]]\{4\}" file What does \? stand for?
7
votes
2answers
10k views

giving grep output to rm

I'm trying to pipe grep output to rm, but it outputs useless stuff. Is any switch required for rm? Or can rm can be provided a regexp directly? ls | grep '^\[Daruchini'| rm rm: missing operand Try ...
1
vote
3answers
947 views

A problem with find and grep

I have defined the following alias in ~/.bashrc: alias fg='find . -name $1 | xargs grep --color $2' in order to write fg "*.txt" " my_text " and find all file that have extension .txt and ...
2
votes
1answer
122 views

The cursor to a match, edit, the cursor to the next match, edit — like in a directory?

Algorithmic blackout: problem: find the files that match problem: stack/buffer the matches move the cursor to the next file of the stack and thereby to the location of the match repeat step 2 ...
8
votes
5answers
600 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 ...
6
votes
7answers
16k views

How do I find text within a file and have it search multiple subfolders?

I'm looking for a function name and the folder structure is deep and there are a lot of files to look though. Usually I go with something like find * | grep functionname but is that the best way?
26
votes
3answers
3k 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 ...
3
votes
1answer
300 views

Trouble with grep -o regex

I'm trying to use a grep command with the --only-matching flag, but it's not behaving as I would expect it to. This command: echo "1/2/3/4/5" | grep -oE "^([^/]+/){0,2}" Gives this output: 1/2/ ...
2
votes
3answers
741 views

Filtering paths to a specific depth

I'm using rsync and the flags -nPaAXz ~/ to check which files are going to be copied. This is far too verbose to make any sense of. How could I filter the output so I view the list of files/folders ...
11
votes
3answers
2k 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 ...
2
votes
3answers
3k views

Extracting tokens from a line of text

Using bash scripting and grep/awk/sed, how can I split a line matching a known pattern with a single character delimiter into an array, e.g. convert token1;token2;token3;token4 into a[0] = token1 … ...
3
votes
2answers
659 views

Using grep/sed/awk to classify log file entries

I need to process a very large log file with many lines in different formats. My goal is to extract unique line entries who have the same starting pattern, e.g. '^2011-02-21.*MyKeyword.*Error', ...
8
votes
4answers
6k 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 ...
9
votes
2answers
3k 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 ...
2
votes
2answers
511 views

grep acting strangely

Grep has been acting strangely on one of my systems (Ubuntu Desktop - all my other systems are Ubuntu Server), and I can't figure out why. I created a control file named text that contains the ...
0
votes
1answer
125 views

Locate a directory within an archive

I have .tgz archive i'm trying to locate a directory within it to extra how do i search within a .tgz i tried the following below but no luck. tar -tzf archive-9.tgz | grep '161519' | xargs tar -Oxzf ...
17
votes
5answers
42k 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 ...
14
votes
9answers
15k views

How can I grep in PDF files?

Is there a way to search pdf files using the power of grep, without converting to text first in Ubuntu?
7
votes
4answers
6k 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?
2
votes
1answer
342 views

How to generate a report summary of messages that triggered a specific DSN code

I use Sendmail on CENTOS 5.x, I hope this is a simple question. =) I need to generate a report summary of messages that triggered a specific DSN code. For example: Jan 11 07:43:34 server-example ...
3
votes
1answer
7k views

Filtering on dates with grep and awk

I have created the alias below in my .bash_aliases file alias auth="grep \"$(date|awk '{print $2,$3}')\" /var/log/auth.log | grep -E '(BREAK-IN|Invalid user|Failed|refused|su|Illegal)'" ...
3
votes
4answers
2k views

How do I check if a file already has line with “contents” in it?

I need to know if a file aready has line with contents X in it, if not append line. here's the code I've tried. if ! $(grep 'eval $(perl -I$HOME/foo/lib/perl5 -Mlocal::lib=$HOME/foo)' ~/.bashrc) then ...
11
votes
5answers
7k 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 ...
17
votes
2answers
2k 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: ...
1
vote
1answer
245 views

How can I grep through files whose name begins with `-`? [duplicate]

I've tried this. grep -r thanks * slave-iv grep: invalid option -- '@' Usage: grep [OPTION]... PATTERN [FILE]... Try `grep --help' for more information. Here's ...
27
votes
7answers
19k 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 ...
8
votes
3answers
942 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 ...
11
votes
7answers
10k 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
2answers
11k 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?
54
votes
7answers
15k views

Is there a basic tutorial for grep, awk and sed? [closed]

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, ...
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
977 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
503 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
259 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
870 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 ...