Shell grep usage

Shell grep usage

A list of grep options

grep -i # ignore case grep -r “words” . ## search words in all files in the current folder, return file name and matched lines; -lr ## output only file names grep -r “wordA|wordB|wordC” grep -lr “wor.” ./log ## search only in files starting with log grep ‘^From: ' /usr/mail/$USER {list your mail} grep ‘[a-zA-Z]’ {any line with at least one letter} grep ‘[^a-zA-Z0-9] {anything not a letter or number} grep ‘[0-9]{3}-[0-9]{4}’ {999-9999, like phone numbers} grep ‘^.$’ {lines with exactly one character} grep ‘“smug”’ {‘smug’ within double quotes} grep ‘"smug”’ {‘smug’, with or without quotes} grep ‘^.’ {any line that starts with a Period “."} grep ‘^.[a-z][a-z]’ {line start with “.” and 2 lc letters} grep -o # show only matched pattern instead of whole line grep -n # add line number grep -v # lines not matching grep -B1 -A1 “PATTERN” FILE # search one line before and one line after

Z. Lu avatar
Z. Lu
Data scientist, bioinformatician, retro fan and web lover.
comments powered by Disqus