Grep
m |
(Common uses of grep) |
||
Line 30: | Line 30: | ||
Looks like only [[root]] does. | Looks like only [[root]] does. | ||
+ | |||
+ | ==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 -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]] | See also: [[strings]] | ||
[[Category:FreeBSD Terminology]] [[Category:System Commands]] | [[Category:FreeBSD Terminology]] [[Category:System Commands]] |
Revision as of 12:40, 17 January 2005
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 passwd | grep csh root:*:0:0:Avatar:/root:/bin/csh samizdata#
Looks like only root does.
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 -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