Grep Regular Expressions Cheat Sheet, Cheat Sheet of Linux skills

Typology: Cheat Sheet

2020/2021

Uploaded on 04/27/2021

myfuture
myfuture ๐Ÿ‡บ๐Ÿ‡ธ

4.4

(18)

258 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Grep Cheat Sheet
Usage
Grep standard output (i.e. a stream of tex t)
grep [-opt
๎˜
ions] โ€˜stringโ€™
Grep the content of a file
grep [-opt
๎˜
ions] โ€˜stringโ€™ filename
Wildcards are accepted in filen
๎˜
ame.
General Regular Expression Processor
Oper๎˜ation Option Exam๎˜ple
Find a string in 1 or more files grep 'string' filename1 filename2 ... filena๎˜men
Case insens๎˜itive search i grep -i 'string' filename
Use regular expres๎˜sions (regex) grep 'regex' filename
Look for words w grep -w 'word' filename
Display n lines after matching string A grep -A n 'string' filename
Display n lines before matching string B grep -B n 'string' filename
Display n lines around matching string C grep -C n 'string' filename
Recu๎˜rsive grep r grep -r 'hacke๎˜rs-๎˜clu๎˜b.cn' /var/l๎˜๎˜og๎˜/๎˜a๎˜pa๎˜๎˜che๎˜๎˜2/๎˜a๎˜r๎˜ch๎˜๎˜ives/
Return all lines which don't match the pa ttern v grep -v 'warning' /var/l๎˜og/๎˜syslog
Use regex e grep -e 'string1' -e 'string2' filename
grep --regexp 'string' filename
Return lines starting with 'al' grep -e '^al' filename
Use extended regex E grep -E 'apach๎˜e|w๎˜hee๎˜l|root' filename
Get lines containing 1+ w grep -E 'w+' filename
Get lines with 3 w in a row (www) grep -E 'w{3}' filename
Get lines containing between 3 and 6 m in a row grep -E 'm{3,6}' filename
Get lines containing jason or jackson grep -E 'ja(s|๎˜cks)on' filename
Count results c grep -c 'error' /var/l๎˜og/๎˜syslog
Display filename l grep -l 'string' /var/log/*
Only show the matching part of the strin g o grep -o 'string' filename
Show line number n grep -n 'string' filename
About grep -E: In basic regular e xpres๎˜sions the meta-c๎˜har๎˜acters โ€˜?โ€™, โ€˜+โ€™, โ€˜{โ€™, โ€˜|โ€™, โ€˜(โ€™, and โ€˜)โ€™ lose their special meaning; instead use the backsl๎˜ashed
versions โ€˜\?โ€™, โ€˜\+โ€™, โ€˜\{โ€™, โ€˜\|โ€™, โ€˜\(โ€™, and โ€˜\)โ€™.
GNU grep -E emulates classic meta-c๎˜har๎˜acters. The command โ€˜grep -E '{1'โ€™ searches for the 2-char๎˜acter string โ€˜{1โ€™ instead of reporting an error.
POSIX allows this behavior as an extension, but portable scripts should avoid it.
pf2

Partial preview of the text

Download Grep Regular Expressions Cheat Sheet and more Cheat Sheet Linux skills in PDF only on Docsity!

Grep Cheat Sheet

Usage Grep standard output (i.e. a stream of text) grep [ -options ] โ€˜stringโ€™ Grep the content of a file grep [ -options ] โ€˜stringโ€™ filename Wildcards are accepted in filename.

General Regular Expression Processor Operation Option Example Find a string in 1 or more files grep 'string' filename1 filename2 ... filename n Case insensitive search i grep -i 'string' filename Use regular expressions (regex) grep 'regex' filename Look for words w grep -w 'word' filename Display n lines after matching string A grep -A n 'string' filename Display n lines before matching string B grep -B n 'string' filename Display n lines around matching string C grep -C n 'string' filename Recursive grep r grep -r 'hackers-club.cn' /var/log/apache2/archives/ Return all lines which don't match the pattern v grep -v 'warning' /var/log/syslog Use regex e grep -e 'string1' -e 'string2' filename grep --regexp 'string' filename Return lines starting with 'al' grep -e '^al' filename Use extended regex E grep -E 'apache|wheel|root' filename Get lines containing 1+ w grep -E 'w+' filename Get lines with 3 w in a row (www) grep -E 'w{3}' filename Get lines containing between 3 and 6 m in a row grep -E 'm{3,6}' filename Get lines containing jason or jackson grep -E 'ja(s|cks)on' filename Count results c grep -c 'error' /var/log/syslog Display filename l grep -l 'string' /var/log/* Only show the matching part of the string o grep -o 'string' filename Show line number n grep -n 'string' filename About grep -E : In basic regular expressions the meta-characters โ€˜?โ€™, โ€˜+โ€™, โ€˜{โ€™, โ€˜|โ€™, โ€˜(โ€™, and โ€˜)โ€™ lose their special meaning; instead use the backslashed versions โ€˜?โ€™, โ€˜+โ€™, โ€˜{โ€™, โ€˜|โ€™, โ€˜(โ€™, and โ€˜)โ€™.

GNU grep -E emulates classic meta-characters. The command โ€˜grep -E '{1'โ€™ searches for the 2-character string โ€˜{1โ€™ instead of reporting an error. POSIX allows this behavior as an extension, but portable scripts should avoid it.

Regular expressions : wildcards

. Any character. ? Optional and can only occur once. ***** Optional and can occur more than once. + Required and can occur more than once. {n} Previous item appears exactly n times. {n,} Previous item appears n times or more. {,m} Previous item appears n times maximum. {n,m} Previous item appears between n and m times. [:alpha:] Any lower and upper case letter. [:digit:] Any number. [:alnum:] Any lower and upper case letter or digit. [:space:] Any whitespace. [A-Za-z] Any lower and upper case letter. [0-9] Any number. [0-9A-Za-z] Any lower and upper case letter or digit.

Regular expressions : anchors and positions ^ Beginning of line. grep '^Once upon a time' /home/livres/lovestory.txt $ End of line. grep 'divorced.$' /home/livres/lovestory.txt ^$ Empty line. < Start of word. grep '<love>' /home/manga/seinen.txt > End of word.

Characters to escape . [ ^ $ '

    • (start of line only)