grep

From LinuxReviews
Jump to navigationJump to search
Konsole.svg

grep is a very old and very handy command line utility that prints any lines matting a regular expression. The most widely used modern grep implementation is GNU grep which supports search recursive file trees, basic regular expressions, extended regular expressions, fixed strings and Perl-compatible regular expressions.

GNU grep has advanced pattern matching control and a lot of options for output control.

History

The first known grep implementation was written by Ken Thompson in the PDP-11 assembly language. It was released in November 1974.

The GNU project, lead by Richard Stallman, begun work on a free software grep implementation in 1985. GNU grep 2.0, released October 1st, 1996, contained parts of regex.h from the original GNU regex library from 1985. We would like to know if/when GNU grep 1.0 was released.

Basics

These examples assume that you are using GNU grep, since that is the most commonly used implementation, and not some ancient version from the 1970s.

The most basic way to use grep is

grep pattern /path/file

grep can even be used for purposes not indicated by the manual. If you want to list filenames and the contents of files you can use as a pattern and -H to show the names of each file matching the "pattern":

$ grep -H '' /sys/devices/system/cpu/vulnerabilities/* | cut -f 5-7 -d '/'
cpu/vulnerabilities/itlb_multihit:Not affected
cpu/vulnerabilities/l1tf:Not affected
cpu/vulnerabilities/mds:Not affected
cpu/vulnerabilities/meltdown:Not affected
cpu/vulnerabilities/spec_store_bypass:Vulnerable
cpu/vulnerabilities/spectre_v1:Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers
cpu/vulnerabilities/spectre_v2:Vulnerable, IBPB: disabled, STIBP: disabled
cpu/vulnerabilities/srbds:Not affected
cpu/vulnerabilities/tsx_async_abort:Not affected

Advanced Examples

You must click the fine "edit" button and add very advanced examples here if you are a grep wizard.

Links

Add your comment
LinuxReviews welcomes all comments. If you do not want to be anonymous, register or log in. It is free.