What is the usage of regular expression in grep?
The grep command is a commonly used command in Linux and Unix systems that is used to search for content that matches a given pattern in files or standard input, and then output that content to standard output.
The basic usage of the grep command is:
grep [选项] 模式 文件名
Commonly used options include:
- -i: Ignore case mixes
- -v: Inverse selection, only output lines that do not match the pattern.
- -n: display line numbers of matching lines
- -c: count the number of matched lines
- -r: search files in folders recursively
- – E: Use extended regular expressions.
- – o: Only output matching parts
Examples:
grep -i "hello" example.txt
This command will search for lines containing “hello” in the example.txt file and output the results to the standard output.
Regular expressions are a powerful tool for pattern matching that can be used in the grep command to implement more complex matching rules. For example, they can be used to match specific patterns such as numbers, letters, spaces, etc.