grep tool
grep is a line filtering tool; filtering based on keywords
Syntax and option
syntax:
grep [选项] “关键字” 文件名
Common options:
- -i is not case sensitive
- -v does not include reverse selection
- -w search by word (exact match)
- -o print matching keywords
- -c count the number of matches
- -n display line number
- -r traverse the directory layer by layer to find
- -A matches the following lines
- -B matches the first few lines of the line
- -C matches a few lines before and after the line
- -l only lists matching file names
- -L lists unmatched file names
- -e Use regular matching
- -E Use extended regular matching
- ^key starts with a keyword
- key$ ends with the keyword
- ^$ matches blank lines
for example:
# 忽略大小写匹配root的行
grep -i 'root' passwd
# 精准匹配单词
grep -w 'ftp' passwd
# 以#开头的行
grep ^# passwd
# 不以#开头的行
grep -v ^# passwd
# 以bash结尾的行
grep bash$ passwd
# 打印空行行号
grep -n ^$ passwd
cut tool
cut is a column interception tool