grep is a command-line tool for searching text patterns in files
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 ...
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?
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 ...
3
votes
3answers
803 views
Show lines matching a pattern and the 4 lines before each
For example, from this file:
CREATE SYNONYM I801XS07 FOR I8010.I801XT07
*
ERROR at line 1:
ORA-00955: name is already used by an existing object
CREATE SYNONYM I801XS07 FOR ...
6
votes
4answers
3k views
How do I recursively grep through compressed archives?
I'm trying to find out what modules use Test::Version in cpan. So I've used minicpan to mirror it. My problem is that I need to iterate through the archives that are downloaded, and grep the files ...
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 ...
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 ...
11
votes
11answers
6k 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
...
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 ...
14
votes
2answers
691 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:
...
5
votes
4answers
1k views
Finding the ID of a process and killing it
When ever I need to kill a background process I do ps -e | grep <process_name>
Which prints something like this 1766 ? 00:00:13 conky , Then I use the process ID to kill it like so kill 1766 .
...
5
votes
7answers
9k 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?
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?
5
votes
2answers
219 views
How do regular expressions differ from wildcards used to filter files
While we use * to denote zero or more previous characters in grep, we use *.c to find all C files when we use it with the ls command like ls *.c. Could someone tell how the use of * differs in these ...
5
votes
4answers
7k views
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 ...
2
votes
2answers
184 views
Searching for string in files
Is there an easy way to search inside 1000s of files in a complex directory structure to find files which contain a specific string within the file?
1
vote
2answers
992 views
How to grep a text recursively on BusyBox 1.0?
How can I find a file containing a certain string on a BusyBox 1.0, if grep -r does not work?
5
votes
1answer
5k views
Return only the portion of a line after a matching pattern
So pulling open a file with cat and then using grep to get matching lines only gets me so far when I am working with the particular log set that I am dealing with. It need a way to match lines to a ...
4
votes
3answers
250 views
Delete the matching line and several more from a file
I have a text file called file_a.txt.
My first command is
grep -A 12 ".production =" file_a.txt
The output is a few block.
Each block of string contains 13 rows
I specifically want to delete all ...
2
votes
2answers
121 views
Use of quotes in GNU grep regular expressions
I could see that certain patterns in the GNU Grep can be enclosed within brackets and certain others need not be. For example, matching the beginning of a word works only if it is enclosed within ...
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?
9
votes
1answer
252 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 ...
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 ...
3
votes
2answers
491 views
How do I use tee to redirect to grep
I don't have much experience of using tee, so I hope this is not very basic.
After viewing one of the answers to this question I came across a strange beheviour with tee.
In order for me to output ...
3
votes
5answers
180 views
evaluate multiple patterns from program output and write into pattern specific files
I have a script outputting some value/numbers and I want to split those into two files. I am looking at something like:
./runme.sh | grep 'ook' >> ook.out | grep 'eek' >> eek.out
Where ...
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 ...
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.
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
;
...
5
votes
2answers
480 views
How to grep on source code without catching comments
I search a way to grep on source code without having sometimes false-positive because of comments. For instance if I search on foo on this .c source code :
/*
* foo has changed [...] and is now a ...
4
votes
4answers
207 views
Most efficient grep method
Currently I am grepping data from a file containing any of the following:
...
4
votes
2answers
740 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
2
votes
3answers
2k views
How to search and replace text in all php-files in a directory and it's subdirectories
I am looking for a shell script that recursively traverses all .php files in a directory and performs a search & replace of a particular text pattern.
The search pattern is quite long ( > 5000 ...
1
vote
3answers
313 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: ...
4
votes
3answers
81 views
Uniquely identifying java processes with same name
I have two java processes which run using the same file name, MyApp.jar (for example).
/usr/java/latest/bin/java -jar MyApp.jar
These jars exist in different places and use different ...
4
votes
4answers
3k views
How to search for a word in entire content of a directory in linux
need to search for something in entire content
I am trying:
find . | xargs grep word
I get error:
xargs: unterminated quote
How to achieve this? Thanks.
3
votes
2answers
346 views
What is the best way to find a list of several strings within a large text file
The short, general question is: In Unix/Linux, what is the best way to find a list of several (about 150) strings within a large text file?
I am asking this to all Unix/Linux experts as a general ...
3
votes
1answer
186 views
grep list of names and information from bigger file
I have two files: one with list of names (500 entries) and other having some more information for each entry in A.txt and extra entries too.
File A.txt (each line is starting with > (fasta format) ...
3
votes
6answers
1k views
Recursive search doesn't work for grep on solaris
On out Solaris server I'm finding that grep -r doesn't work, which is the usual way that I used grep. It seems that egrep is the same.
Given that I have no control over the machine, is there a way to ...
2
votes
2answers
112 views
find all cpp files and list only those files with names that match a pattern (regex) does not work
I have a whole repository of files and I am trying to get a list of files that match a certain criteria.
For example, let us say that I want to take all the files that have the string foo1 and ...
2
votes
1answer
218 views
How to grep lines which does not begin with “#” or “;”?
I want to grep smb.conf and see only lines which are not commented.
2
votes
1answer
952 views
Delete last character in a word but only if the character is there - in bash
How to remove last character only if it's there?
input:
OpenOffice.org/m
openOffice.org/ozm
Pers.
Pfg.
phil.
Prof.
resp.
Roonstr./m
roonstr./ozm
desired output:
OpenOffice.org
openOffice.org
...
2
votes
4answers
4k views
How to find the total number of occurrences of text and files with find command
I am trying to run the find command to find the total number of occurrences of a particular text string and also the number of files which has this text string.
What I have right now is this command.
...
2
votes
2answers
535 views
How can I retain the console input in mplayer when reading from stdin?
I'm playing around with the command line interface of mplayer. I'd like to script it in the following way
find /some/path/ -type f | grep -vif blacklist | mplayer -shuffle -playlist -
where ...
2
votes
3answers
259 views
Returning only the portion of a line after a matching pattern
I have a file (file_name) which contains exactly one occurance of the string Result:, at the start of a line. I want to print all the characters after the string Result: in that line until I encounter ...
2
votes
2answers
319 views
How do I show all files in a directory except those that have a certain extension AND display them in multiple columns?
Since I write in Python a lot it's kind of annoying to see .pyc files all the time and I would like to just hide them at this point. I tried the following, but I can't seem to figure out how to get it ...
2
votes
1answer
1k views
Time/Date grep for 15 mins
I need to grep a specific type of line in a specific time of the log, do you have any ideas?
Here's my working script:
cat *.log |grep -E '2011-06-30 (1[0-1]:[0-1][0-5]|10:16)'| grep -ach '0110 ...
2
votes
1answer
246 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/
...
1
vote
2answers
1k views
Find all lines in a file with a certain character at a certain position
I have a lab question asking me to use grep with a regex pattern to match every word where the third letter from the beginning of the line is an "a" and save it with a redirect. How can I do that?
1
vote
1answer
488 views
Difficulty making a regular expression to find at least 2 occurrences of a character in a file
I have a file containing random codes. Each code has ten characters in it, and I am trying to grep codes in the file that have at least 2 occurrences of a character. I am doing this:
grep DD* ...
0
votes
1answer
48 views
How to ask for specific parameters with grep?
I need to look in /usr/share/dict/words for a 5-letter word that starts with d or D, followed by a lowercase vowel, and ends with s. I have tried everything but to no avail.
