扫码阅读
手机扫码阅读

三剑客之 grep

77 2024-07-20

我们非常重视原创文章,为尊重知识产权并避免潜在的版权问题,我们在此提供文章的摘要供您初步了解。如果您想要查阅更为详尽的内容,访问作者的公众号页面获取完整文章。

查看原文:三剑客之 grep
文章来源:
咸鱼运维杂谈
扫码关注公众号
Grep Command Summary

Grep Command Summary

Introduction: Grep is a command-line utility on UNIX systems for searching a given pattern within files and outputting the matching lines. Created in 1974 by Ken Thompson, it is an essential tool for regular expression handling.

Basic Usage:

The basic grep format is:

grep [options] pattern [file or directory]

Main Options:

  • -E: Supports extended regular expressions
  • -i: Ignores case
  • -n: Prints line numbers
  • -o: Outputs only the matched parts of the line
  • -c: Outputs the count of matching lines
  • -B: Outputs n lines before the match
  • -A: Outputs n lines after the match
  • -C: Outputs n lines around the match
  • -v: Outputs lines that do not match
  • -e: Allows multiple patterns

Regular Expressions:

Using regular expressions can enhance the functionality of grep:

  • *: Matches the previous character 0 or more times
  • ^: Matches the start of a line
  • $: Matches the end of a line
  • .*: Matches any number of any characters
  • [ ]: Matches any character inside the brackets
  • [^ ]: Excludes any character inside the brackets
  • \<: Anchors the match to the start of a word
  • \>: Anchors the match to the end of a word
  • {m,n}: Matches the preceding character at least m and at most n times

Examples:

Examples are based on the 'passwd.txt' file and demonstrate various grep functionalities, such as:

  • Filtering out blank lines or comments
  • Searching with or without case sensitivity
  • Outputting only the matching content or the line number
  • Showing lines before or after the matching line
  • Using multiple search patterns with the -e option

想要了解更多内容?

查看原文:三剑客之 grep
文章来源:
咸鱼运维杂谈
扫码关注公众号