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

learn more… | top users | synonyms

2
votes
4answers
1k views

Grep for a string with list of files as source

I have a plain text file, in every line is a path to a file. Now I have to grep for a string in these files. Is there any way to use this file as "search source" for grep? Or do I have to copy and ...
3
votes
1answer
1k views

Escaping of meta characters in basic/extended posix regex strings in grep

Is it possible to escape all meta characters of a string inside a variable before passing it to grep? I know similar question has been asked before on SE (here) and also a good explanation here, but I ...
1
vote
1answer
193 views

egrep command which shows all the authors and subjects of mails contained in a directory

Anyone know of an egrep command which will show the authors and subjects of all mails contained in some directory?
2
votes
2answers
6k 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?
5
votes
3answers
5k views

Search for values in 2 columns from CSV

I have a csv files with say 20 odd columns. I need to get the 14th and 15th column values where value in 14th column is say "VALUE1" and value in 15th column is "VALUE2". My condition gets satisfied ...
3
votes
1answer
6k views

grep and escaping a dollar sign

I want to know which files have the string $Id$. grep \$Id\$ my_dir/mylist_of_files returns 0 occurrences. I discovered that I have to use grep \$Id$ my_dir/mylist_of_files Then I see that the ...
0
votes
1answer
873 views

Egrep: contains and does not contain

egrep -i '12-12.*\swarning' log.file | cless The above will produce two lines with the date 12-12, followed by random characters, a white space, then Warning and more text. 2011-12-12 textetc ...
3
votes
2answers
711 views

Concatenate specific files into one single file

In a folder containing X files, I need to concatenate Y files (where X > Y) together into a single text file. I have the filenames (of Y files) that I need to concatenate. Can anyone please help me ...
4
votes
2answers
24k views

How to run grep on a single column?

I want to grep the output of my ls -l command: -rw-r--r-- 1 root root 1866 Feb 14 07:47 rahmu.file -rw-r--r-- 1 rahmu user 95653 Feb 14 07:47 foo.file -rw-r--r-- 1 rahmu user ...
8
votes
5answers
23k 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 ...
1
vote
3answers
345 views

grep equivalent of the kwrite regex [A-Z][A-Z]+

So, it took me ages, but I finally learned to think in terms of regular expressions, thanks to using them in kwrite. But I still don't know how to translate that knowledge to grep. I love my grep, ...
7
votes
3answers
6k views

grep: Trailing backslash

I want to do a grep for \resources\. How do I do this? I've tried: grep \resources\ grep \\resources\\ grep "\resources\" grep "\\resources\\" None of these work.
11
votes
4answers
7k 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 ...
9
votes
2answers
294 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, ...
0
votes
1answer
74 views

grep *stringA*[number]

I was wondering how can I grep an expression of the type *stringA*[number] In other words, I want to target lines that have the following pattern: anything + stringA + anything + [number] For ...
7
votes
3answers
11k 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 ...
3
votes
1answer
958 views

git grep colors differ from grep custom colors

I've setup grep colors in my ~/.bashrc : export GREP_COLORS='ms=01;34:mc=01;34:sl=:cx=:fn=35:ln=32:bn=32:se=36' They work for grep --color=auto grep --color=always Unfortunately, those custom ...
2
votes
2answers
454 views

xargs grep suggestion

grep -v "\<Swap" instruments.log | awk '{ idx=index($0, "MasterId="); masterId=substr($0, idx+length("MasterId=")+1); masterId=substr(masterId,1,index(masterId,"L")-3); print masterId; }' | xargs ...
1
vote
1answer
1k 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* ...
2
votes
2answers
964 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 ...
1
vote
1answer
2k 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?
12
votes
11answers
10k 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
1answer
483 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 ...
3
votes
3answers
3k 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
2k views

Command-line tool to search docx files

Is there a command-line tool to text-search a docx file? I tried grep, but it doesn't work with docx even though it works fine with txt and xml files. I could convert the docx to txt first, but I'd ...
1
vote
2answers
4k views

Using grep to match only a match group in a regular expression?

I need to set my keyboard layout with setxkbmap before launching Wine games, as I use Dvorak for typing and this breaks every game's controls. What I'd like to do is simply write a script that grabs ...
1
vote
4answers
613 views

grep seems to ignore --include

man grep says grep [OPTIONS] PATTERN [FILE...] . . . --include=GLOB Search only files whose base name matches GLOB (using wildcard matching as described under --exclude). ...
6
votes
4answers
3k 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 . ...
3
votes
5answers
258 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 ...
0
votes
1answer
588 views

grep searchstring /etc/crontab strange behavior in BASH script

I am working on a BASH script which creates automated backups via the crontab. I am trying to use myVARIABLE=`grep backup /etc/crontab` to return lines containing "backup" if an entry has already ...
6
votes
3answers
7k views

How can I find all files that do NOT contain a text string?

What concise command can I use to find all files that do NOT contain a text string? I tried this (using -v to invert grep's parameters) with no luck: find . -exec grep -v -l shared.php {} \; ...
4
votes
6answers
6k views

Recursive search doesn't work for grep on solaris

On our Solaris server I'm finding that grep -r doesn't work, which is the usual way that I use grep. It seems that egrep has the same behavior. Given that I have no control over the machine, is there ...
9
votes
11answers
7k 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
4answers
10k views

Grep: how to add an “OR” condition?

I am a total regex newbie. I find it confusing and I avoid it like the plague (wanna take some guesses as to why I'm not good at it?). But I suspect it might be the solution to my question. Which ...
4
votes
6answers
1k views

Grep a directory and return list with line numbers

I'm currently trying to learn more about bash scripting and all of that fun stuff, and I pieced together this little command: find $path | xargs grep -n $pattern | awk '{print $1}' While this DOES ...
2
votes
1answer
378 views

Getting data section with maximum element

I have some data stored in JSON-like format. { { value1: 14, value2: 12, value3: 1 }, { value1: 4, value3: -1 } } How can I get subsection ...
2
votes
2answers
1k views

How to treat a file as a single line with grep to apply a regexp search pattern?

I want to match everything that is between some lines with regexp but not this that match the start and the end. This sound to me as positive lookbehind and positive lookahead start text bla ...
13
votes
1answer
479 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 ...
2
votes
3answers
847 views

How to cut the output to only gather the filename and get-parameter?

I have multiple files with multiple links that are formatted like this: <a href="http://example.com/fnord.layername.html?parameter=FOO-_-BAR-_-FNORD" class="poit"> <img ... /> ...
5
votes
2answers
4k views

Looking for a Java class in a set of JARs with find, unzip, grep

I was trying to find the JAR containing a Java Class. JARs are in zip format. My first attempt was: $ find -name "*3.0.6.RELEASE.jar" | xargs -l1 unzip -l \ | grep stereotype.Controller ...
4
votes
1answer
1k views

How can I match the page break character in a regular expression?

Nano calls it ^L, but of course, typing something like $ grep -v "^\^L" file doesn't work. Its unicode codepoint is 000C. How can I match it in a regular expression?
15
votes
4answers
44k views

count number of lines, bash grep

Hello I am grepping results to file, and then doing cat to show its content on the screen, I want to know how many lines of results I have in my results file and the += it to some counter. What will ...
3
votes
2answers
5k views

Search string in many files on HP-UX

I need to find which files (they can have space in the filename) of a directory contains a string using only sh and system's commands (Perl is not an option). For a few files, this command works ...
1
vote
2answers
3k views

Why does adding a colon break this grep pattern?

I executed a search with grep, but it doesn't work like I expected it to. I have the following lines in a file: blacklists/redirector/domains:needyoutube.com ...
1
vote
2answers
6k views

Finding files containing certain strings under a directory [duplicate]

Possible Duplicate: How do I find text within a file and have it search multiple subfolders? I would like to find out all the files under mydir/ directory whose content contains the string ...
1
vote
1answer
1k views

How to filter wvdial/pppd terminal output

I use wvdial to connect to the net with with my USB modem. It works correctly, and I am quite happy with it. When I start it in XFCE I have it set to output to a borderless, transparent terminal so ...
1
vote
2answers
436 views

pattern search and display the last occuring pattern?

I have a log file containing startup and shutdown times for everday. I want to see the last pattern pattern for startup and shutdown(which is located at the end of the file being updated everyday). ...
3
votes
1answer
1k views

How to use a value returned by a script in grep filter?

I'm on FreeBSD's csh. I want to form a row with domains of one user and then use this row to filter exim maillog to get only mail from/to this user's domains. I wrote a script to form a row from a ...
8
votes
1answer
23k 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 ...
2
votes
3answers
924 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 ...