Linux Awk command All In One
Linux Awk command All In One
shell script
https://www.gnu.org/software/gawk/manual/gawk.html
Awk
AWK (awk /ɔːk/
) is a domain-specific
language designed for text processing
and typically used as a data extraction and reporting tool. Like sed
and grep
, it is a filter
, and is a standard feature of most Unix-like
operating systems.
AWK (awk /ɔːk/
) 是一种专门用于文本处理
的领域特定
语言,通常用作数据提取
和报告工具
。
与 sed
和 grep
一样,它是一个过滤器
,并且是大多数类 Unix
操作系统的标准功能。
https://en.wikipedia.org/wiki/AWK
awk
language
The AWK Programming Language.pdf
published on 1988
https://ia803404.us.archive.org/0/items/pdfy-MgN0H1joIoDVoIC7/The_AWK_Programming_Language.pdf
awk
languageThe AWK Programming Language.pdf
1988
API
https://tldp.org/LDP/abs/html/awk.html
鳥哥的 Linux 私房菜
awk
https://linux.xgqfrms.xyz/linux_basic/1010index.htm
https://linux.xgqfrms.xyz/linux_basic/0330regularex.htm#awk
AWK
Tutorials
awk 语法
https://www.runoob.com/linux/linux-comm-awk.html
$ awk [选项参数] 'script' var=value file(s)
或
$ awk [选项参数] -f scriptfile var=value file(s)
awk 内建变量
变量 | 描述 |
---|---|
$n | 当前记录的第n个字段,字段间由FS分隔 |
$0 | 完整的输入记录 |
ARGC | 命令行参数的数目 |
ARGIND | 命令行中当前文件的位置(从0开始算) |
ARGV | 包含命令行参数的数组 |
BEGIN | ❓指定开始的分隔符 |
CONVFMT | 数字转换格式(默认值为%.6g)ENVIRON环境变量关联数组 |
ERRNO | 最后一个系统错误的描述 |
FIELDWIDTHS | 字段宽度列表(用空格键分隔) |
FILENAME | 当前文件名 |
FNR | 各文件分别计数的行号 |
FS | 字段分隔符(默认是任何空格) |
IGNORECASE | 如果为真,则进行忽略大小写的匹配 |
NF | 一条记录的字段的数目 |
NR | 已经读出的记录数,就是行号,从1开始 |
OFMT | 数字的输出格式(默认值是%.6g) |
OFS | 输出字段分隔符,默认值与输入字段分隔符一致。 |
ORS | 输出记录分隔符(默认值是一个换行符) |
RLENGTH | 由match函数所匹配的字符串的长度 |
RS | 记录分隔符(默认是一个换行符) |
RSTART | 由match函数所匹配的字符串的第一个位置 |
SUBSEP | 数组下标分隔符(默认值是/034) |
awk 运算符
运算符 | 描述 |
---|---|
= += -= *= /= %= ^= **= | 赋值 |
?: | C条件表达式 |
|| | 逻辑或 |
&& | 逻辑与 |
~ 和 !~ | 匹配正则表达式和不匹配正则表达式 |
< <= > >= != == | 关系运算符 |
空格 | 连接 |
+ - | 加,减 |
* / % | 乘,除与求余 |
+ - ! | 一元加,减和逻辑非 |
^ *** | 求幂 |
++ -- | 增加或减少,作为前缀或后缀 |
$ | 字段引用 |
in | 数组成员 |
awk 选项参数
:
选项参数
:-F
/ FS or --field-separator
指定
输入文件分隔符
,FS 是一个字符串或者是一个正则表达式,如 -F:
。
-v
var=value or --asign
var=value
赋值
一个用户定义变量
。
-f
scripfile or --file
scriptfile
从脚本
文件中读取
awk 命令。
-mf
nnn and -mr
nnn
对nnn值设置内在限制
-mf 选项限制块
最大数目, 分配给nnn的;
-mr 选项限制记录
的最大数目, 分配给nnn的。
⚠️ 这两个功能是 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 报告信息的版本
usage
# 行匹配语句 awk '{...}' 只能用单引号
$ awk '{[pattern] action}' {filenames}
# -F 等价于 awk 内置变量 FS ✅
# 使用 -F 指定分割字符 🚀
$ awk -F, '{action}' {filenames}
# 使用 awk 内建变量 FS, 指定分割字符 💩
$ awk 'BEGIN{FS=","} {action}' {filenames}
# 使用多个分隔符 /多次分隔符 ⚠️
# 先使用`空格`分割; 然后对分割结果, 再使用`,`分割;
$ awk -F '[ ,]' '{action}' {filenames}
# 设置变量
$ awk -v key=value '{[pattern] action}' {filenames}
# $ awk -v a=1 '{print $1,$1+a}' log.txt
# awk -f {awk脚本} {文件名}
$ awk -f cal.awk log.txt
按
whitespace
或TAB
分割,输出文本中每行的1
、4
项匹配 (字符串/单词)
eric@rpi4b:~/Desktop $ mkdir awk-scripts
eric@rpi4b:~/Desktop $ cd awk-scripts/
eric@rpi4b:~/Desktop/awk-scripts $ touch log.txt
eric@rpi4b:~/Desktop/awk-scripts $ vim log.txt
eric@rpi4b:~/Desktop/awk-scripts $ awk '{print $1,$4}' log.txt
2 a
3 like
This's
10 orange,apple,mongo
eric@rpi4b:~/Desktop/awk-scripts $ cat log.txt
2 this is a test
3 Do you like awk
This's a test
10 There are orange,apple,mongo
# $1,$4 ❌
eric@rpi4b:~/Desktop/awk-scripts $ awk '{printf "%-8s %-10s\n",$1,$4}' log.txt
2 a
3 like
This's
10 orange,apple,mongo
eric@rpi4b:~/Desktop/awk-scripts $
空格
分词 /制表符
分词
# $1,$3 ✅
$ awk '{printf "%-8s %-10s\n",$1,$3}' log.txt
2 is
3 you
This's test
10 are
eric@rpi4b:~/Desktop/awk-scripts $ cat log.txt
2 this is a test
3 Do you like awk
This's a test
10 There are orange,apple,mongo
eric@rpi4b:~/Desktop/awk-scripts $ awk -F, '{print $1,$2}' log.txt
2 this is a test
3 Do you like awk
This's a test
10 There are orange apple
eric@rpi4b:~/Desktop/awk-scripts $ awk 'BEGIN{FS=","} {print $1,$2}' log.txt
2 this is a test
3 Do you like awk
This's a test
10 There are orange apple
eric@rpi4b:~/Desktop/awk-scripts $
eric@rpi4b:~/Desktop/awk-scripts $ awk -F '[ ,]' '{print $1,$2,$5}' log.txt
2 this test
3 Do awk
This's a
10 There apple
eric@rpi4b:~/Desktop/awk-scripts $ cat log.txt
2 this is a test
3 Do you like awk
This's a test
10 There are orange,apple,mongo
eric@rpi4b:~/Desktop/awk-scripts $
eric@rpi4b:~/Desktop/awk-scripts $ awk -v a=1 '{print $1,$1+a}' log.txt
2 3
3 4
This's 1
10 11
eric@rpi4b:~/Desktop/awk-scripts $ cat log.txt
2 this is a test
3 Do you like awk
This's a test
10 There are orange,apple,mongo
demos
awk
# shellcheck shell=dash
___x_cmd____main(){
[ "$#" -gt 0 ] || {
return
}
local op="$1"; shift
case "$op" in
esac
}
https://github.com/web-full-stack/x-cmd/blob/main/mod/api/lib/main
# Author: Li Junhao l@x-cmd.com
# shellcheck shell=sh disable=SC2039,SC1090,SC3043,SC2263
# license: GPLv3
x log init api
xrc:mod:lib api main \
provider/ali provider/aws provider/ucloud provider/cloudflare \
provider/gh provider/gt provider/gl provider/tea \
sign/ali_sign sign/aws/ecs_sign sign/ucloud/ecs_sign
___x_cmd_api(){
local op="${1}"; [ -z "$op" ] || shift
case "$op" in
gh) ___x_cmd_api_gh "$@" ;;
gt) ___x_cmd_api_gt "$@" ;;
gl) ___x_cmd_api_gl "$@" ;;
tea) ___x_cmd_api_tea "$@" ;;
ali|aws|ucloud)
x openssl 1>/dev/null 2>&1
___x_cmd_api_"${op}" "$@" ;;
cloudflare) ___x_cmd_api_cloudflare "$@" ;;
*) _____x_cmd_api_help; return;;
esac
}
_____x_cmd_api_init(){
___X_CMD_API_TMP="${___X_CMD_API_TMP:-"${___X_CMD_ROOT_TMP}/api"}"
x mkdirp "$___X_CMD_API_TMP"
}
# shellcheck disable=2120
_____x_cmd_api_help(){
x help -m api "$@" 1>&2
return 1
}
_____x_cmd_api_init
xrc setmain ___x_cmd_api
https://github.com/web-full-stack/x-cmd/blob/main/mod/api/latest
.jso
file
.jso - JISO 压缩 PSP ISO 映像
JSO磁盘映像文件与PSP ISO Compressor 相关。
JSO文件是 JISO 压缩的 PSP ISO 映像。它用于压缩转储的索尼 PlayStation Portable (PSP) UMD 游戏。
PSP ISO 压缩器似乎不再受支持。
.jso
file
JSO磁盘映像文件与PSP ISO Compressor 相关。
JSO文件是 JISO 压缩的 PSP ISO 映像。它用于压缩转储的索尼 PlayStation Portable (PSP) UMD 游戏。
PSP ISO 压缩器似乎不再受支持。
{
"#name"
:
{
"x api"
:
null
,
"en"
:
"Sending API requests by parsing the OpenAPI document"
,
"cn"
:
"通过解析 OpenAPI 文档发送 API 请求"
}
,
"#tag"
:
[
"inner"
]
,
"#desc"
:
{
"en"
:
"TODO Sending API requests by parsing the OpenAPI document"
,
"cn"
:
"TODO 项目未开始 通过解析 OpenAPI 文档发送 API 请求"
}
,
"#subcmd:Cloud"
:
[
"aws"
,
"ali"
,
"ucloud"
]
,
"#subcmd:Git"
:
[
"gh"
,
"gl"
,
"gt"
,
"tea"
]
,
"ali"
:
{
"#desc"
:
{
"en"
:
"Sending requests to Ali Cloud API"
,
"cn"
:
"发送 阿里云 API 请求"
}
,
"$ref"
:
"x-advise://api/data/ali.jso"
}
,
"aws"
:
{
"#desc"
:
{
"en"
:
"Sending requests to Ali Cloud API"
,
"cn"
:
"发送 AWS 云 API 请求"
}
,
"$ref"
:
"x-advise://api/data/aws.jso"
}
,
"ucloud"
:
{
"#desc"
:
{
"en"
:
"Sending an AWS cloud service API request"
,
"cn"
:
"发送 UCloud API 请求"
}
,
"$ref"
:
"x-advise://api/data/ucloud.jso"
}
,
"gh"
:
{
"#desc"
:
{
"en"
:
"Sending requests to Github API"
,
"cn"
:
"发送 Github API 请求"
}
,
"#synopsis"
:
[
{
"x api gh [get|post|put|patch|del] <API shortcut endpoint> [curl option]"
:
null
}
]
,
"#tldr"
:
[
{
"cmd"
:
"x api gh get /repos/{owner}/{repo}/collaborators"
,
"en"
:
"list collaborators in the target repository"
,
"cn"
:
"罗列目标仓库成员"
}
,
{
"cmd"
:
"x api gh get /repos/{owner}/{repo}/releases -G --data-urlencode per_page=5"
,
"en"
:
"list releases and limit 5 total number in the target repository"
,
"cn"
:
"罗列目标仓库发行版本列表并限制数量为5条"
}
]
,
"#tip:note"
:
[
{
"en"
:
"Github access token is setted through environment variable `___X_CMD_API_GH_TOKEN` or `x gh token`"
,
"cn"
:
"Github access token 通过设置环境变量 `___X_CMD_API_GH_TOKEN` 或 `x gh token` 获取"
}
]
,
"$ref"
:
"x-advise://api/data/gh.jso"
}
,
"gt"
:
{
"#desc"
:
{
"en"
:
"Sending requests to Gitee API"
,
"cn"
:
"发送 Gitee API 请求"
}
,
"#synopsis"
:
[
{
"x api gt [get|post|put|patch|del] <API shortcut endpoint> [curl option]"
:
null
}
]
,
"#tldr"
:
[
{
"cmd"
:
"x api gt get /repos/{owner}/{repo}/collaborators"
,
"en"
:
"list collaborators in the target repository"
,
"cn"
:
"罗列目标仓库成员"
}
,
{
"cmd"
:
"x api gt get /repos/{owner}/{repo}/releases -G --data-urlencode per_page=5"
,
"en"
:
"list releases and limit 5 total number in the target repository"
,
"cn"
:
"罗列目标仓库发行版本列表并限制总数为5条"
}
]
,
"#tip:note"
:
[
{
"en"
:
"Gitee access token is setted through environment variable `___X_CMD_API_GT_TOKEN` or `x gt token`"
,
"cn"
:
"Gitee access token 通过设置环境变量 `___X_CMD_API_GT_TOKEN` 或 `x gt token` 获取"
}
]
,
"$ref"
:
"x-advise://api/data/gt.jso"
}
,
"gl"
:
{
"#desc"
:
{
"en"
:
"Sending requests to Gitlab API"
,
"cn"
:
"发送 Gitlab API 请求"
}
,
"#synopsis"
:
[
{
"x api gl [get|post|put|patch|del] <API shortcut endpoint> [curl option]"
:
null
}
]
,
"#tldr"
:
[
{
"cmd"
:
"x api gl get /projects/{owner}%2F{project}/members"
,
"en"
:
"list member in the target project"
,
"cn"
:
"罗列目标项目成员"
}
,
{
"cmd"
:
"x api gl get /projects/{owner}%2F{project}/releases -G --data-urlencode sort=asc"
,
"en"
:
"list releases and sort in ascending order on the target project"
,
"cn"
:
"罗列目标项目发行版本列表并以正序排序"
}
]
,
"#tip:note"
:
[
{
"en"
:
"Gitlab access token is setted through environment variable `___X_CMD_API_GL_TOKEN` or `x gl token`"
,
"cn"
:
"Gitlab access token 通过设置环境变量 `___X_CMD_API_GL_TOKEN` 或 `x gl token` 获取"
}
]
,
"$ref"
:
"x-advise://api/data/gl.jso"
}
,
"tea"
:
{
"#desc"
:
{
"en"
:
"Sending requests to Gitea API"
,
"cn"
:
"发送 Gitea API 请求"
}
,
"#synopsis"
:
[
{
"x api tea [get|post|put|patch|del] <API shortcut endpoint> [curl option]"
:
null
}
]
,
"#tldr"
:
[
{
"cmd"
:
"x api tea get /repos/{owner}/{repo}/collaborators"
,
"en"
:
"list member in the target repository"
,
"cn"
:
"罗列目标仓库成员"
}
,
{
"cmd"
:
"x api tea get /repos/{owner}/{repo}/releases -G --data-urlencode per_page=5"
,
"en"
:
"list releases and limit 5 total number in the target repository"
,
"cn"
:
"罗列目标仓库发行版本列表并限制数量为5条"
}
]
,
"#tip:note"
:
[
{
"en"
:
"Gitea access token is setted through environment variable `___X_CMD_API_TEA_TOKEN` or `x tea token`"
,
"cn"
:
"Gitea access token 通过设置环境变量 `___X_CMD_API_TEA_TOKEN` 或 `x tea token` 获取"
}
]
,
"$ref"
:
"x-advise://api/data/tea.jso"
}
}
https://github.com/web-full-stack/x-cmd/blob/main/adv/api/advise.jso
https://github.com/web-full-stack/x-cmd/tree/main/adv/api/data
AWK 工作原理
# BEGIN [pattern] [END]
$ awk 'BEGIN{ commands } pattern{ commands } END{ commands }'
AWK 工作流程可分为三个部分:
读输入文件之前
执行的代码段(由BEGIN
关键字标识)。
主循环执行
输入文件的代码段
读输入文件之后
的代码段(由END
关键字标识)。
$ awk 'BEGIN{printf "序号\t名字\t课程\t分数\n"} {print}' marks.txt
序号 名字 课程 分数
1) 张三 语文 80
2) 李四 数学 90
3) 王五 英语 87
eric@rpi4b:~/Desktop/awk-scripts $ cat marks.txt
1) 张三 语文 80
2) 李四 数学 90
3) 王五 英语 87
eric@rpi4b:~/Desktop/awk-scripts $
序号 名字 课程 分数
1) 张三 语文 80
2) 李四 数学 90
3) 王五 英语 87
序号 名字 课程 分数
1) 张三 语文 80
2) 李四 数学 90
3) 王五 英语 87
✅序号 ✅名字 ✅课程 ✅分数
序号 名字 课程 分数
3) 王五 英语 87
序号 名字 课程 分数
名字 课程 分数
-------------
王五 英语 87
-------------
名字 课程 分数
<<EOF
$ awk 'BEGIN{printf "序号\t名字\t课程\t分数\n"} {print}' marks.txt
$ awk 'BEGIN{printf "序号\t名字\t课程\t分数\n"} {print} END{printf "✅序号\t✅名字\t✅课程\t✅分数\n"}' marks.txt
$ awk 'BEGIN{printf "序号\t名字\t课程\t分数\n"} /英语/ {print $1,$2,$3,$4} END{printf "序号\t名字\t课程\t分数\n"}' marks.txt
$ awk 'BEGIN{printf "名字 课程 分数\n-------------\n"} /英语/ {print $2,$3,$4} END{printf "-------------\n名字 课程 分数\n"}' marks.txt
EOF
https://www.runoob.com/w3cnote/awk-work-principle.html
awk blogs
https://www.digitalocean.com/community/tutorials/awk-command-linux-unix
https://www.freecodecamp.org/news/the-linux-awk-command-linux-and-unix-usage-syntax-examples/
grep
更适合单纯的查找
或匹配
文本 (过滤)sed
更适合编辑
匹配到的文本 (替换)awk
更适合格式化文本
,对文本进行较复杂
格式处理
x-cmd
https://x.com/xgqfrms/status/1711044475320143876?s=20
refs
https://github.com/web-full-stack/x-cmd/issues/1
https://www.cnblogs.com/xgqfrms/tag/awk/
©xgqfrms 2012-2025
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17750551.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2022-10-09 Overseas Chinese Community All In One
2022-10-09 SQL 查询语句: 模糊查询 All In One
2021-10-09 js Array.find vs Array.filter All In One
2021-10-09 Linux Bash shell 脚本定时器 All In One
2020-10-09 电子白板
2020-10-09 Nuxt.js SSR Optimizing Tips
2020-10-09 TypeScript & WebAssembly All In One