美文网首页
shell-awk命令

shell-awk命令

作者: 哆来咪发都不会 | 来源:发表于2019-08-19 20:26 被阅读0次

awk比sed更加强大,能对行中的段进行操作。

语法
Usage: awk [POSIX or GNU style options] -f progfile [--] file ...
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]
    -e 'program-text'   --source='program-text'
    -E file         --exec=file
    -g          --gen-pot
    -h          --help
    -L [fatal]      --lint[=fatal]
    -n          --non-decimal-data
    -N          --use-lc-numeric
    -O          --optimize
    -p[file]        --profile[=file]
    -P          --posix
    -r          --re-interval
    -S          --sandbox
    -t          --lint-old
    -V          --version
常见用法
指定分隔符过滤文件(不指定时默认为空格或空白字符)
[root@localhost wang]# cat 123.txt 
abc:jsak:12njsdjq:kljlkhsa:hahah:wang
wanghuan:kjskjfd:popl9:q2j2q:ruhr

#以:为分隔符,打印出第一段
[root@localhost wang]# awk -F ':' '{print $1}' 123.txt 
abc
wanghuan
[root@localhost wang]# 

#打印第1段、第2段
[root@localhost wang]# awk -F ':' '{print $1,$2}' 123.txt 
abc jsak
wanghuan kjskjfd
[root@localhost wang]# 

#段之间指定符号,不改变文件
[root@localhost wang]# awk -F ':' '{print $1"##"$2}' 123.txt 
abc##jsak
wanghuan##kjskjfd
[root@localhost wang]# 

#以:为分隔符,打印出所有段,$0所有段
[root@localhost wang]# awk -F ':' '{print $0}' 123.txt 
abc:jsak:12njsdjq:kljlkhsa:hahah:wang
wanghuan:kjskjfd:popl9:q2j2q:ruhr
[root@localhost wang]# 

#不指定分隔符时,默认以空格或空白字符为分隔符
[root@localhost wang]# cat 1.txt 
1 2
a b
111 ood
[root@localhost wang]# awk '{print $1}' 1.txt 
1
a
111
[root@localhost wang]# 
#以一个或多个空格或者%作为分隔符
awk -F '[ %]+' '{PRINT $1}' 1.txt

匹配功能(类似grep)
[root@localhost wang]# cat haha.txt 
13:abc:def
123:def:edf
abc:123:qwer
acd:qwe:123
def:123
12345:123
abcdef:def

#过滤出所有段中包含123的行
[root@localhost wang]# awk '/123/' haha.txt 
123:def:edf
abc:123:qwer
acd:qwe:123
def:123
12345:123

#过滤出第1段中包含123的行
[root@localhost wang]# awk -F ':' '$1 ~ /123/' haha.txt 
123:def:edf
12345:123

#过滤出第2段中包含123的行
[root@localhost wang]# awk -F ':' '$2 ~ /123/' haha.txt 
abc:123:qwer
def:123
12345:123

#过滤出第3段中含有e+的行
[root@localhost wang]# awk -F ':' '$3 ~ /e+/' haha.txt 
13:abc:def
123:def:edf
abc:123:qwer

#打印出包含123的行中的第1段、第3段,以及包含def的行中的第2段、第3段
[root@localhost wang]# awk -F ':' '/123/ {print $1,$3} /def/ {print $2,$3}' haha.txt 
abc def
123 edf
def edf
abc qwer
acd 123
def 
123 
12345 
def 

#打印出包含123或abc的行中所有段
[root@localhost wang]# awk -F ':' '/123|abc/ {print $0}' haha.txt 
13:abc:def
123:def:edf
abc:123:qwer
acd:qwe:123
def:123
12345:123
abcdef:def
[root@localhost wang]# 

#打印出第1段等于123的行
[root@localhost wang]# awk -F ':' '$1==123 {print $1}' haha.txt 
123
[root@localhost wang]# awk -F ':' '$1==123 {print $0}' haha.txt 
123:def:edf

#第1段小于125的行
[root@localhost wang]# awk -F ':' '$1<=125 {print $0}' haha.txt 
13:abc:def
123:def:edf
[root@localhost wang]# 
NF、NR、OFS
#OFS指定打印出的分隔符
[root@localhost wang]# awk -F ':' '{OFS="#"} $1<140 {print $1,$2}' haha.txt 
13#abc
123#def
[root@localhost wang]#.

#NR,打印出行号
[root@localhost wang]# awk -F ':' '$1<140 {print NR":"$0}' haha.txt 
1:13:abc:def
2:123:def:edf
[root@localhost wang]# 

#打印出前2行
[root@localhost wang]# awk -F ':' 'NR<=2 {print NR":"$0}' haha.txt 
1:13:abc:def
2:123:def:edf
[root@localhost wang]# 

#NF,打印出段的数量
[root@localhost wang]# awk -F ':' '$1<140 {print NF":"$0}' haha.txt 
3:13:abc:def
3:123:def:edf
[root@localhost wang]# 

相关文章

网友评论

      本文标题:shell-awk命令

      本文链接:https://www.haomeiwen.com/subject/rybesctx.html