Questions tagged [grep]

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

The grep utility searches a pattern (regular expression) in a text file. There are many options to control how matches are performed and how to display matches.

External references

Tutorials

Further reading

See also grep on Unix & Linux Stack Exchange for more detailed reading.

326 questions
7
votes
3 answers

echo based on grep result

I need a one liner which displays 'yes' or 'no' whether grep finds any results. I have played with grep -c, but without success.
takeshin
  • 1,471
  • 3
  • 21
  • 28
6
votes
2 answers

Grep Not Working (on some files)?

I have a MSSQL database dump I'm trying to grep through. However, grep does not seem to work on it, at all. Here is a snippet of the file. ... ALTER DATABASE [Documents] SET RECURSIVE_TRIGGERS OFF GO ALTER DATABASE [Documents] SET …
djneely
  • 335
  • 3
  • 11
6
votes
4 answers

grep ouput filepath with file modified date?

Is it possible to make the grep command to output file paths with file modified date like so: 12-02-2015 /file/path/to/the/file 16-02-2015 /file/path/to/the/file 25-02-2015 /file/path/to/the/file 03-04-2015…
user3257264
  • 61
  • 1
  • 1
  • 2
6
votes
4 answers

Kill process by command name

I would like to kill a java process based on the command name... Whats the best way to do it? (i.e. when you do ps -f the name in the CMD column).
DD.
  • 3,114
  • 11
  • 35
  • 50
6
votes
3 answers

OS/2 grep had a great feature where it would show you n lines BEFORE the search item was found. Is there an equivalent in unix anywhere?

Solaris would be preferable, but I'll take linux or something I can compile. Does such a beast exist?
Stu
  • 2,198
  • 2
  • 16
  • 23
6
votes
1 answer

grep range of dates - apache log

I'm wondering how could I do the following greps: grep Apache log only for a range of dates, assume from 5/Nov/2010 to 5/Dec/2010 and grep Apache log starting from `15/Nov/2010 until the last log entry. Thanks
Charles
  • 63
  • 1
  • 1
  • 4
6
votes
6 answers

How to remove a tagged block of text in a file?

How can I remove all instances of tagged blocks of text in a file with sed, grep, or another program? If I have a file which contains: random text // START TEXT internal text // END TEXT more random // START TEXT asdf …
EmpireJones
  • 358
  • 1
  • 4
  • 13
5
votes
3 answers

Extracting the date of the latest message from an mbox file

How would I go about extracting the "Date:" header for the latest received message in an mbox file? Note that it's not simply an issue of grep'ing for the latest occurrence of "^Date:", since it might as well be the date from a quoted reply, not…
5
votes
1 answer

How to grep a continuous stream, with paging

I posted this to Superuser but got no takers: https://superuser.com/questions/832578/how-to-grep-a-continuous-stream-with-paging I want to take a log file and filter out some irrelevant log entries, like ones at the INFO level. The above Stack…
phasetwenty
  • 198
  • 2
  • 7
5
votes
1 answer

grep is inconsistently defaulting to grep -P?

I have a script that does some housekeeping that works perfectly well when invoked from an interactive shell, but did nothing when invoked by cron. To troubleshoot this I started a shell with a 'blank' environment with the command: env -i /bin/bash…
Sammitch
  • 2,111
  • 1
  • 21
  • 35
5
votes
4 answers

iptables - Clear all PREROUTING rules with a specific destination address

I have a script that adds iptable PREROUTING rules. They all have the same to address. When I run this: iptables --list PREROUTING -t nat I see output like this: DNAT tcp -- anywhere 165.193.122.18 tcp dpt:https…
user548971
  • 259
  • 1
  • 3
  • 10
5
votes
6 answers

grep question, how to filter several type of patterns?

I'm console newbie. As I know, matching multiple patterns like this: aaa|bbb But, | character is pipe on console, so how can I specify those multiple patterns for grep?
Eonil
  • 10,459
  • 16
  • 36
  • 54
5
votes
3 answers

Best way to remove text from the beginning of a huge file

I have a huge MySQL backup file (from mysqldump) with the tables in alphabetical order. My restore failed and I want to pick up where I left off with the next table in the backup file. (I have corrected the problem, this isn't really a question…
Christopher Schultz
  • 1,105
  • 2
  • 12
  • 22
4
votes
2 answers

How to show specific luns from lsscsi command?

I need to grep specific luns from the lsscsi command. For example: [root@e15l1 ~]# lsscsi 4 0 1 | awk '{print $1,$6}' | head [4:0:1:0] - [4:0:1:1] /dev/sdab [4:0:1:2] /dev/sdj [4:0:1:3] /dev/sdz [4:0:1:4] /dev/sdk [4:0:1:12] /dev/sdo [4:0:1:13]…
igor
  • 141
  • 1
4
votes
2 answers

cygwin grep running 680 times faster via ssh than via remote desktop

Running grep by opening Cygwin terminal via Microsoft Remote Desktop to Windows Server 1012 R2(same as natively?): Administrator@MYSERV /cygdrive/d/bin/beta $ time grep -inowf matchfile_431184247462809.temp infile_431184247462809.temp > delme real …
Muposat
  • 121
  • 9