Grep

Search input files for lines containing a match to a given pattern list. Matching lines are (by default) copied to standard output.


grep [options] pattern input_file_names

Multiple Patterns


grep -e patternA -e patternB input_file_names
grep --regex=patternA --regex=patternB input_file_names
or

grep -f pattern_file_name input_file_names
grep --file=pattern_file_name input_file_names
Where pattern_file_name contains a list of patterns, separated by endlines.

Options

Ignore case
-i or --ignore-case

Select non-matching lines instead
-v or --invert-match

Select whole word matches only
-w or --word-regexp

Select whole line matches only
-x or --line-regexp

Output just the count of matching lines per file
-c or --count

Output list of files containing no matches only
-L or --files-without-match

Output list of files containing a match only
-l or --files-with-matches

Stop searching a file after x matching lines are found
-m x or --max-count=x

Print just the matching section of lines
-o or --only-matching

Print line number by each output (starting at 1)
-n or --line-number

Include x lines after each matching line
-A x or --after-context=x

Include x lines before each matching line
-B x or --before-context=x

Include x lines before and after each matching lines
-C x or --context=x or -x
Sed

Sed = Stream EDitor. It is for modifying files automatically.

Substitution

Replace text based on regular expressions. By default, sed will only replace the FIRST occurance of a substring in each line.

Replace substring "current" with string "new" throughout old_filename. Save the output to new_filename.

sed s/current/new/ old_filename > new_filename

Use quotes if you have metacharacters in the regular expression.

sed 's/current/new/' old_filename > new_filename

The regular expression delimiter is the first character after s. You can use any delimiter.

sed 's_/usr/bin_/common/bin_' old_filename > new_filename

sed 's:/usr/bin:/common/bin:' old_filename > new_filename
The error "unterminated 's' command" means you forgot a delimiter.

Use & to refer to the matched string, so you can use it in the replacement.

sed 'a/[aeiou]*/(&)/' old_filename > new_filename

Use escaped () to mark multiple patterns for later use. They will be refered to as \1 \2...\9. 9 is the limit.

sed 's:\([aeiou]*\).*:\1:' old_filename > new_filename

Addressing

Addressing specified which lines to apply the replacement to.

--only make replacements in lines that contain 'IF'
sed '/IF/s/current/new/' old_filename > new_filename

Flags

The global flag replaces ALL instances of matching strings in each line.

sed 's/current/new/g' old_filename > new_filename

Replace just the Xth instance in each line.

sed 's/current/new/X' old_filename > new_filename

--just the 4th 'current' is replaced
sed 's/current/new/4' old_filename > new_filename

Option -n means don't print output. Flag p means print only lines that contain a match.

sed -n 's/current/new/p' old_filename > new_filename

--print just lines with matches, like grep
sed -n 's/current/ p' old_filename > new_filename

--print just lines without a match
sed -n 's/current/ !p' old_filename > new_filename

Flag w means print only the matches. If you use multiple flags, w must be the last one.

sed -n 's/current/new/w' old_filename > new_filename

The ignore case flag.

sed 's/current/new/I' old_filename > new_filename

Options

Make multiple substitutions at once.

sed -e 's/A/a/' -e 's/B/b/' old_filename > new_filename

--or specify a file of endline-delimited substitution expressions
sed -f pattern_filename old_filename > new_filename

Restriction

Only apply changes to the Xth line.

sed 'X s/current/new/' old_filename > new_filename

--only the 3rd line
sed '3 s/current/new/' old_filename > new_filename

--only lines 3 through 90 inclusive
sed '3,90 s/current/new/' old_filename > new_filename

--only lines 3 through end of file
sed '3,$ s/current/new/' old_filename > new_filename

Only apply changes where the line starts with '#'.

sed '/^#/ s/current/new/' old_filename > new_filename

Only apply changes that occur between START and STOP. So, the from line that includes START to the next line that includes STOP. This can occur multiple times per file.

sed '/START/,/STOP/ s/current/new/' old_filename > new_filename

--from the 3rd line to the next instance of /STOP/
sed '3,/STOP/ s/current/new/' old_filename > new_filename

Delete all lines from the 3rd to the end of file.

sed '3,$ d' old_filename > new_filename