grep is a command-line tool for searching text patterns in files
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 ...
5
votes
3answers
1k views
how to execute lines coming from a grep result?
I've got a text file with certain installation instructions, and I know I can grep for a unique occurrence in the file. For example, the text file has something like:
MYDIR=`find /home/user -name ...
0
votes
1answer
494 views
Curl and grep html text
Is there a way to grep html text from a curl request and output grab the value that is between two html tags?
More specifically the input value of a form?
1
vote
3answers
230 views
vim/emacs plugin to view recursive grep search search results?
Suppose I have a file formatted like this:
file1:123
file2:4444
some/other/file:2233
This represents the search results from a grep (i.e. file:line-number).
I'd like to have a vim plugin which ...
2
votes
2answers
183 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?
5
votes
4answers
1k views
Find a string only in a specific file inside subdirectories
Let's say I need to find the function GetTypes() in all C# source file (.cs) the directories/subdirectories.
I used grep -rn GetTypes *.cs, but I got an error with grep: *.cs: No such file or ...
2
votes
2answers
462 views
sed, grep, or tr command that returns only Latin characters from a UTF-8 file
I am working with the text of the 300 tang poems, which unfortunately is a single file containing both Chinese and English. Since I am interested in 'extracting' the English, I hope to use sed, ...
2
votes
3answers
330 views
pad a number with zeroes
I need have put 0 on the results of my grep so my script format will be fine and i don't have any idea how to do it. here's my grep result :
261 : 261 = 0 | 1192 : 1184 = 8 |
283 : 283 = 0 | 666 : ...
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 ...
2
votes
2answers
116 views
How costly is case insensitive search as compared to case sensitive search?
I dont think grep -i is exponentially (wrt to the number of characters to be grepped) more costly (time wise) than a normal grep because the run times are not too different.
But theoretically it ...
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 ...
5
votes
1answer
105 views
Search for a text in Linux but don't display the complete line in results
I'm using grep to do this, like:
grep someText file.log
But this returns the complete line in results. If someText is found in the middle of a line I don't want to ouput the whole line, just the ...
4
votes
2answers
3k views
Reverse grep to get the last couple of occurrences in a file
Say I want to grab the last 5
mentions of cron in /var/log/syslog.log looking over the grep manual it seems like I can't get grep to search in reverse. I can however grab the most recent occurrences.
...
3
votes
1answer
117 views
Is there a archive of old man pages? specifically for the features available for grep in early 'ed'
This is just a bit of history rummaging... I'd like to know how different today's grep is to the original implementation, introduced by Ken Thompson in ed. which evolved to ex, which then became vi...
...
2
votes
1answer
924 views
Grep - ^(\s*$) vs ^\s*$
I want to search for a field that only has spaces. When counting, this two commands gave me different results:
grep "^\s*$" -E -c
grep "^(\s*$)" -E -c
Another thing. What is the correct way to say ...
1
vote
0answers
84 views
How to search for file contents [duplicate]
Possible Duplicate:
Finding a substring in files across subdirectories with a single built-in command?
I know that I can use grep for a single file, but what about a whole directory tree?
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 ...
3
votes
2answers
99 views
Generate suggestions for errors from a lookup dictionary
EDIT: the files were changed to tsv to deal better with spaces in text fields
I have 2 csv files in the following form:
File 1: availableText.csv (can be very big)
"id1" , "text1-1"
"id1" , ...
5
votes
5answers
344 views
How can I recursively search directories for a pattern and just print out file names and line numbers
Currently I am using grep as follows:
grep -lr <PATTERN> .
This gives me the following output, containing a list of all files whose contents (not name) contain :
./path/to/file1
...
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 ...
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 ...
1
vote
3answers
312 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
5answers
9k 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. ...
4
votes
2answers
990 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 ...
5
votes
3answers
2k 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?
4
votes
2answers
3k 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
4answers
621 views
A problem with find and grep
I have dafined the following 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 contain " ...
2
votes
1answer
117 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
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 ...
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?
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 ...
2
votes
1answer
245 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
468 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 ...
8
votes
3answers
867 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
2k 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 … ...
2
votes
1answer
447 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', ...
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 ...
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 ...
2
votes
2answers
333 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
115 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 ...
12
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 ...
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?
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?
2
votes
0answers
259 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 ...
2
votes
1answer
4k 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
666 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
...
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 ...
14
votes
2answers
688 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
203 views
How can I grep through files whose name begins with `-`?
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 ...
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 ...
