pavement

Grep

From FreeBSDwiki
(Difference between revisions)
Jump to: navigation, search
(Common uses of grep)
(we've been through this before - if you're n00b enough to need that example of grep, you're n00b enough to need an example of redirection as well =))
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
== The basics ==
 +
 
[[grep]] returns results that match a string. You can use it to find a line in a file in a directory, by having grep run through the directory and show you what matches your query. Grep accepts input from the commandline, but is more often fed input in conjunction with other commands, such as [[find]], [[ls]] or [[cat]]:
 
[[grep]] returns results that match a string. You can use it to find a line in a file in a directory, by having grep run through the directory and show you what matches your query. Grep accepts input from the commandline, but is more often fed input in conjunction with other commands, such as [[find]], [[ls]] or [[cat]]:
  
Line 25: Line 27:
 
Let's say you want to know which of your users has [[csh]] as their shell:
 
Let's say you want to know which of your users has [[csh]] as their shell:
  
  samizdata# '''cat passwd | grep csh'''
+
  samizdata# '''cat csh | grep /etc/passwd'''
 
  root:*:0:0:Avatar:/root:/bin/csh
 
  root:*:0:0:Avatar:/root:/bin/csh
 
  samizdata#
 
  samizdata#
  
Looks like only [[root]] does.
+
Looks like only [[root]] does. (You could also have used grep by itself to do the same thing - '''grep csh /etc/passwd''' - but it's important to understand and get used to the idea of redirecting output from one command to the next.)
  
==Common uses of grep==
+
==Producing results like [[find]] does==
 +
 
 +
Let's say that you just want to see a list of all the files containing the word PATTERN, in this directory or in any directory below it:
 +
 
 +
server# '''grep -lr PATTERN *'''
 +
 
 +
Perhaps you wanted to ''delete'' all files in this directory and all its subdirectories which contains the word PATTERN?
 +
 
 +
server# '''grep -lr PATTERN * | [[xargs]] rm'''
 +
 
 +
Or move any file in this directory and all its subdirectories which contains a word ending in "temp" to the /tmp directory?
 +
 
 +
server# '''grep -Elr ".*temp" * | [[xargs]] -J % mv % /tmp'''
 +
 
 +
==Other common uses of grep==
  
 
'''grep -l pattern *''' to get only the filename of files that match in the current directory.
 
'''grep -l pattern *''' to get only the filename of files that match in the current directory.
Line 38: Line 54:
  
 
'''grep -l goodpattern * | grep -v badpattern ''' This use a [[Pipe]] to get grep output as the input of another grep; it gets you lines having goodpattern but not badpattern in the current directory (the "-v" means "non-matching instead of matching"). You can switch the two grep around if you like, but the directory to be searched must go to the leftmost grep after the pattern.
 
'''grep -l goodpattern * | grep -v badpattern ''' This use a [[Pipe]] to get grep output as the input of another grep; it gets you lines having goodpattern but not badpattern in the current directory (the "-v" means "non-matching instead of matching"). You can switch the two grep around if you like, but the directory to be searched must go to the leftmost grep after the pattern.
 +
 +
'''grep -E "cat|dog|giraffe" pets.txt''' This will search the file pets.txt for any line including the patterns "cat", "dog", OR "giraffe".  Very handy.  Note the '''-E''' argument - that specifies an extended regular expression; the "or" pipe construct doesn't work with basic regular expressions ('''grep''''s default regexp type).
  
 
'''grep -f No-No-list *''' to get patterns from to match from the file 'No-No-list' instead of the command line. There can be hundreds of patterns listed in it (one per line). This is useful when looking for large sets of common typos, autodetection of swear words and known spam on wikis, and finding code snippets that have to be modified for compatibility purposes.
 
'''grep -f No-No-list *''' to get patterns from to match from the file 'No-No-list' instead of the command line. There can be hundreds of patterns listed in it (one per line). This is useful when looking for large sets of common typos, autodetection of swear words and known spam on wikis, and finding code snippets that have to be modified for compatibility purposes.

Latest revision as of 18:26, 22 August 2007

[edit] The basics

grep returns results that match a string. You can use it to find a line in a file in a directory, by having grep run through the directory and show you what matches your query. Grep accepts input from the commandline, but is more often fed input in conjunction with other commands, such as find, ls or cat:

samizdata# ls -la /etc | grep .conf
-rw-r--r--   1 root  wheel      1163 Jun  5  2003 apmd.conf
-rw-r--r--   1 root  wheel       271 Jun  5  2003 auth.conf
-rw-r--r--   1 root  wheel      2965 Jun  5  2003 devd.conf
-rw-r--r--   1 root  wheel      2073 Jun  5  2003 devfs.conf
-rw-r--r--   1 root  wheel       267 Jun  5  2003 dhclient.conf
-rw-r--r--   1 root  wheel      5159 Jun  5  2003 inetd.conf
-rw-r--r--   1 root  wheel      6521 Jun  5  2003 login.conf
-rw-r--r--   1 root  wheel     65536 Jun  5  2003 login.conf.db
-rw-r--r--   1 root  wheel       503 Jun  5  2003 mac.conf
-rw-r--r--   1 root  wheel       201 Jun  5  2003 make.conf
-rw-r--r--   1 root  wheel       963 Jun  5  2003 manpath.config
-rw-r--r--   1 root  wheel       963 Jun  5  2003 manpath.config.bak
-rw-r--r--   1 root  wheel       783 Jun  5  2003 netconfig
-rw-r--r--   1 root  wheel      1871 Jun  5  2003 newsyslog.conf
-rw-------   1 root  wheel      1701 Jun  5  2003 nsmb.conf
-rw-r--r--   1 root  wheel       630 Aug 23 21:46 rc.conf
-rw-r--r--   1 root  wheel        45 Jun  5  2003 resolv.conf
-rw-r--r--   1 root  wheel       367 Jun  5  2003 sysctl.conf
-rw-r--r--   1 root  wheel      1329 Jun  5  2003 syslog.conf
-rw-r--r--   1 root  wheel      2023 Jun  5  2003 usbd.conf

Let's say you want to know which of your users has csh as their shell:

samizdata# cat csh | grep /etc/passwd
root:*:0:0:Avatar:/root:/bin/csh
samizdata#

Looks like only root does. (You could also have used grep by itself to do the same thing - grep csh /etc/passwd - but it's important to understand and get used to the idea of redirecting output from one command to the next.)

[edit] Producing results like find does

Let's say that you just want to see a list of all the files containing the word PATTERN, in this directory or in any directory below it:

server# grep -lr PATTERN *

Perhaps you wanted to delete all files in this directory and all its subdirectories which contains the word PATTERN?

server# grep -lr PATTERN * | xargs rm

Or move any file in this directory and all its subdirectories which contains a word ending in "temp" to the /tmp directory?

server# grep -Elr ".*temp" * | xargs -J % mv % /tmp

[edit] Other common uses of grep

grep -l pattern * to get only the filename of files that match in the current directory.

grep -i pattern * to get lines that match inside files in the current directory. The "-i" is for case-insensitive so you'll match "Pattern" and "PATTERN" too.

grep -l goodpattern * | grep -v badpattern This use a Pipe to get grep output as the input of another grep; it gets you lines having goodpattern but not badpattern in the current directory (the "-v" means "non-matching instead of matching"). You can switch the two grep around if you like, but the directory to be searched must go to the leftmost grep after the pattern.

grep -E "cat|dog|giraffe" pets.txt This will search the file pets.txt for any line including the patterns "cat", "dog", OR "giraffe". Very handy. Note the -E argument - that specifies an extended regular expression; the "or" pipe construct doesn't work with basic regular expressions (grep's default regexp type).

grep -f No-No-list * to get patterns from to match from the file 'No-No-list' instead of the command line. There can be hundreds of patterns listed in it (one per line). This is useful when looking for large sets of common typos, autodetection of swear words and known spam on wikis, and finding code snippets that have to be modified for compatibility purposes.

See also: strings

Personal tools