Yuhang He's Blog

Some birds are not meant to be caged, their feathers are just too bright.

grep is often used together with sed, awk to process file. It is often used to search specified pattern among file pools. grep: global search regular expression. Its intuitive usage is to search patterns with regular expression and further print searched patterns out. It always works as:

If you want to search multiple patterns at the same time, you may turn to |, egrep or grep -E. For example:

grep searches patterns in various files, sometimes we have to recursively search the pattern from files at various directories. Or we just need to return the file names containing the pattern. We can use -l, -r to achieve this:

regular expression is often used in grep. Similar to other relevant regular expression awk, sed, grep often uses regular expression to approximately search pattern. Some common regular expressions include: . means must include one character, * means repeat the prior character 0 or N times. For example:

To further specify the number of character we want to search, we should turn to \{\}. For example:

Another way to search for lines containing pattern with more than one occurrence is to use +