Unix Regular Expression Cheat Sheet



  • Shell Regular Expressions The Unix shell recognises a limited form of regular expressions used with lename substitution?: match any single character.: match zero or more characters. : match list of characters in the list speci ed ! : match characters not in the list speci ed Examples: 1 ls. 2 cp a-z. lower/ 3 cp !a-z. upper digit/ 5 / 52.
  • /regex/ Match lines matching the regular expression regex. Addr1,+N Will match addr1 and the N lines following addr1. Cregexc Match lines matching the regular expression regex. The c may be any character. Addr1,N Will match addr1 and the lines following addr1 until the next line whose input line number is a multiple of N.

Grep is a handy command-line utility that enables you to search text or files.

More formally: Grep (Global Regular Expression Print) is a utility for searching plain-text datasets for lines that match a given regular expression.

Grep was developed for Unix, but is now available for all unix-like systems (e.g. Linux).

AWK cheat sheets can make working with data in UNIX a lot easier. AWK is a UNIX programming language that few techs have mastered. I know what you’re saying: “One more programming language to.

Basic Command Syntax

Ignore Case

Use -i:

Recursive Search

Grep Regular Expression Cheat Sheet

Regex

Use -r:

Grep Regex Cheat Sheet

To suppress the filename. use the -h option. This example shows suppressed filenames and sends the result output to a file:

Regex Cheat Sheet Pdf

Search for Whole Words

Grep will return lines that contain the target string - it may be a fragment of another string. To return only lines containing the target string as a distinct word, use the -w option:

Regex Cheat Sheet

Search for many words requires egrep (extended grep), or escaping the pipe character:

Regular Expression Commands

References