Three Musketeers of Text Processing (grep, sed, awk)

One, grep

1.1 Introduction to grep

  grep is a powerful text search tool that can search for text using specific pattern matching (including regular expressions) and output matching lines by default.
  The grep family includes grep, egrep and fgrep. Both egrep and fgrep are extensions of grep, egrep supports extended regular expressions to achieve a text filtering function similar to grep, which is equivalent to grep -E; fgrep command is used to find one or more strings or phrases that match a given string or phrase Line in the file. The query speed of fgrep is faster than the grep command, but it is not flexible enough: it can only find fixed text, not regular expressions, which is equivalent to grep -F.

1.2 Syntax and parameters

grep [-abcEFGhHilLnqrsvVwxy][-A<显示列数>][-B<显示列数>][-C<显示列数>][-d<进行动作>][-e<范本样式>][-f<范本文件>][--help][范本样式][文件或目录...]

-a 或 --text : 不要忽略二进制的数据。
-A<显示行数> 或 --after-context=<显示行数> : 除了显示符合范本样式的那一列之外,并显示该行之后的内容。
-b 或 --byte-offset : 在显示符合样式的那一行之前,标示出该行第一个字符的编号。
-B<显示行数> 或 --before-context=<显示行数> : 除了显示符合样式的那一行之外,并显示该行之前的内容。
-c 或 --count : 计算符合样式的列数。
-C<显示行数> 或 --context=<显示行数>或-<显示行数> : 除了显示符合样式的那一行之外,并显示该行之前后的内容。
-d <动作> 或 --directories=<动作> : 当指定要查找的是目录而非文件时,必须使用这项参数,否则grep指令将回报信息并停止动作。
-e<范本样式> 或 --regexp=<范本样式> : 指定字符串做为查找文件内容的样式。
-E 或 --extended-regexp : 将样式为延伸的正则表达式来使用。
-f<规则文件> 或 --file=<规则文件> : 指定规则文件,其内容含有一个或多个规则样式,让grep查找符合规则条件的文件内容,格式为每行一个规则样式。
-F 或 --fixed-regexp : 将样式视为固定字符串的列表。
-G 或 --basic-regexp : 将样式视为普通的表示法来使用。
-h 或 --no-filename : 在显示符合样式的那一行之前,不标示该行所属的文件名称。
-H 或 --with-filename : 在显示符合样式的那一行之前,表示该行所属的文件名称。
-i 或 --ignore-case : 忽略字符大小写的差别。
-l 或 --file-with-matches : 列出文件内容符合指定的样式的文件名称。
-L 或 --files-without-match : 列出文件内容不符合指定的样式的文件名称。
-n 或 --line-number : 在显示符合样式的那一行之前,标示出该行的列数编号。
-o 或 --only-matching : 只显示匹配PATTERN 部分。
-q 或 --quiet或--silent : 不显示任何信息。
-r 或 --recursive : 此参数的效果和指定"-d recurse"参数相同。
-s 或 --no-messages : 不显示错误信息。
-v 或 --revert-match : 显示不包含匹配文本的所有行。
-V 或 --version : 显示版本信息。
-w 或 --word-regexp : 只显示全字符合的列。
-x --line-regexp : 只显示全列符合的列。
-y : 此参数的效果和指定"-i"参数相同。

1.3 Example

Insert picture description here

Two, sed

2.1 Introduction to sed

  Sed is a "non-interactive" character stream-oriented editor. The content of multiple files and multiple lines can be processed at the same time, the entire file can be input to the screen without changing the original file, and the content that only matches the pattern can be input to the screen. You can also modify the original file, but the results will not be returned on the screen.

2.2 Syntax and parameters

sed [-hnV][-e<script>][-f<script文件>][文本文件]

参数说明:

-e<script>或--expression=<script> 以选项中指定的script来处理输入的文本文件。
-f<script文件>或--file=<script文件> 以选项中指定的script文件来处理输入的文本文件。
-h或--help 显示帮助。
-n或--quiet或--silent 仅显示script处理后的结果。
-V或--version 显示版本信息。

动作说明:

a :新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
c :取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d :删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
i :插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p :打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行。
s :替换,可以直接进行替换的工作

2.3 Example

Insert picture description here

Three, awk

3.1 Introduction to awk

  Awk is a powerful text analysis tool. Compared with grep search and sed editing, awk is particularly powerful when it analyzes data and generates reports. Simply put, awk reads the file line by line, slices each line with a space as the default separator, and performs various analysis and processing on the cut part.

3.2 Syntax and parameters

Basic grammar

awk 'BEGIN{ print "头部分"} pattern{ 命令} END{ print "尾部分"}'

pattern
正则表达式:正则表达式用/ /加在中间
关系表达式:大于,小于,大于等于,小于等于,等于,不等于
模式匹配表达式:用 ~ 表示匹配, !~ 表示不匹配
BEGIN语句块,pattern语句块,END语句块

action
操作由一个或多个命令、函数、表达式组成,之间由换行符或分号隔开,并位于大括号内,主要部分是 :
- 变量或数组赋值
- 输出命令
- 内置函数
- 控制流语句
- 基本结构

parameter

-F fs or --field-separator fs
指定输入文件折分隔符,fs是一个字符串或者是一个正则表达式,如-F:。
-v var=value or --asign var=value
赋值一个用户定义变量。
-f scripfile or --file scriptfile
从脚本文件中读取awk命令。
-mf nnn and -mr nnn
对nnn值设置内在限制,-mf选项限制分配给nnn的最大块数目;-mr选项限制记录的最大数目。这两个功能是Bell实验室版awk的扩展功能,在标准awk中不适用。
-W compact or --compat, -W traditional or --traditional
在兼容模式下运行awk。所以gawk的行为和标准的awk完全一样,所有的awk扩展都被忽略。
-W copyleft or --copyleft, -W copyright or --copyright
打印简短的版权信息。
-W help or --help, -W usage or --usage
打印全部awk选项和每个选项的简短说明。
-W lint or --lint
打印不能向传统unix平台移植的结构的警告。
-W lint-old or --lint-old
打印关于不能向传统unix平台移植的结构的警告。
-W posix
打开兼容模式。但有以下限制,不识别:/x、函数关键字、func、换码序列以及当fs是一个空格时,将新行作为一个域分隔符;操作符**和**=不能代替^和^=;fflush无效。
-W re-interval or --re-inerval
允许间隔正则表达式的使用,参考(grep中的Posix字符类),如括号表达式[[:alpha:]]。
-W source program-text or --source program-text
使用program-text作为源代码,可与-f命令混用。
-W version or --version
打印bug报告信息的版本。
ARGC               命令行参数个数
ARGV               命令行参数排列
ENVIRON            支持队列中系统环境变量的使用
FILENAME           awk浏览的文件名
FNR                浏览文件的记录数
FS                 设置输入列分隔符,等价于命令行 -F选项
NF                 浏览记录的列的个数
$NF               最后一列的信息
NR                 已读的记录数
OFS                输出列分隔符
ORS                输出记录分隔符
RS                 控制记录分隔符
$0变量是指整条记录。$1表示当前行的第一个域,$2表示当前行的第二个域,......以此类推。

3.3 Example

#最近常用命令
awk '{print $1}' ~/.bash_history | sort | uniq -c | sort -rn | head -n 10
#查看机器的每个远程链接机器的连接数
netstat -antu | awk '$5 ~ /[0-9]:/{split($5, a, ":"); ips[a[1]]++} END {for (ip in ips) print ips[ip], ip | "sort -k1 -nr"}'
#查看最近登陆最多的IP
last | awk '{S[$3]++} END{for(a in S ) {print S[a],a}}' |uniq| sort -rh

Four, summary

  • grep is more suitable for simple search or matching text
  • sed is more suitable for editing the matched text
  • awk is more suitable for formatting text, and more complex formatting of text

Guess you like

Origin blog.csdn.net/qq_42979842/article/details/107360680