Skip to content

Update Linux.md #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions notes/Linux.md
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,38 @@ $ printf '%10s %5i %5i %5i %8.2f \n' $(cat printf.txt)
是由 Alfred Aho,Peter Weinberger 和 Brian Kernighan 创造,awk 这个名字就是这三个创始人名字的首字母。

awk 每次处理一行,处理的最小单位是字段,每个字段的命名方式为:\$n,n 为字段号,从 1 开始,\$0 表示一整行。
```sh
Usage: awk [POSIX or GNU style options] [--] 'program' file ...
POSIX options: GNU long options: (standard)
-f progfile --file=progfile
-F fs --field-separator=fs
-v var=val --assign=var=val
Short options: GNU long options: (extensions)
-b --characters-as-bytes
-c --traditional
-C --copyright
-d[file] --dump-variables[=file]
-D[file] --debug[=file]
-e 'program-text' --source='program-text'
-E file --exec=file
-g --gen-pot
-h --help
-i includefile --include=includefile
-l library --load=library
-L[fatal|invalid|no-ext] --lint[=fatal|invalid|no-ext]
-M --bignum
-N --use-lc-numeric
-n --non-decimal-data
-o[file] --pretty-print[=file]
-O --optimize
-p[file] --profile[=file]
-P --posix
-r --re-interval
-s --no-optimize
-S --sandbox
-t --lint-old
-V --version
```

示例:取出最近五个登录用户的用户名和 IP。首先用 last -n 5 取出用最近五个登录用户的所有信息,可以看到用户名和 IP 分别在第 1 列和第 3 列,我们用 \$1 和 \$3 就能取出这两个字段,然后用 print 进行打印。

Expand Down Expand Up @@ -1098,6 +1130,16 @@ root 0
bin 1
daemon 2
```
示例: 过滤日志文件中返回码及其个数
```sh
awk -F"|" '{print $7}' /opt/logs/interface.log | sort | uniq -c | sort -nrk 1 | awk '{printf "返回码==%-10s , 返回码个数==%s\n",$2,$1}'

示例: 查询某指定文本日志文件中,根据某分隔符的第几个字段的值为指定值的文本在该文本日志件中的打印
```sh
grep "example" logfile.txt | awk -F',' '$2=="example" {print}'
```
> `grep "example" logfile.txt`:首先使用grep命令查找包含"example"的行,并将结果输出到标准输出。
> `awk -F',' '$2=="example" {print}'`:然后使用awk命令对grep的输出进行处理。`-F','`指定分隔符为逗号。`$2=="example"`表示只处理第2个字段为"example"的行。`{print}`表示打印符合条件的行。

awk 变量:

Expand Down