AWK命令总结

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AWK命令总结相关的知识,希望对你有一定的参考价值。

 AWK主要是擅长取列
[[email protected] ~]# cat oldboy.log  
oldboy oldgirl
dd     fff
ddd    fff
[[email protected] ~]# awk ‘{print $1}‘ oldboy.log
oldboy
dd
ddd
[[email protected] ~]# awk ‘{print $2}‘ oldboy.log 
oldgirl
fff
fff
3.6.2 指定分隔符,NF代表列数,$NF代表那一列的内容
[[email protected] oldboy]# tail -10 /etc/passwd > abc
[[email protected] oldboy]# cat abc
stu13:x:513:513::/home/stu13:/bin/bash
stu14:x:514:514::/home/stu14:/bin/bash
stu15:x:515:515::/home/stu15:/bin/bash
stu16:x:516:516::/home/stu16:/bin/bash
stu17:x:517:517::/home/stu17:/bin/bash
stu18:x:518:518::/home/stu18:/bin/bash
stu19:x:519:519::/home/stu19:/bin/bash
stu20:x:520:520::/home/stu20:/bin/bash
aa:x:521:521::/home/aa:/bin/bash
bb:x:522:522::/home/bb:/bin/bash
[[email protected] oldboy]#
[[email protected] oldboy]# awk -F ":" ‘{print $NF}‘ abc
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/bash
/bin/bash
[[email protected] oldboy]#
[[email protected] oldboy]# awk -F ":" ‘{print $1,$2,$3}‘ abc    
stu13 x 513
stu14 x 514
stu15 x 515
stu16 x 516
stu17 x 517
stu18 x 518
stu19 x 519
stu20 x 520
aa x 521
bb x 522
[[email protected] oldboy]#
3.6.3 取行,$NR代表行号
[[email protected] oldboy]# awk ‘NR==5‘ abc   
stu17:x:517:517::/home/stu17:/bin/bash
[[email protected] oldboy]#
3.6.4 有些类似sed的取行
[[email protected] oldboy]# sed -n ‘5p‘ abc
stu08:x:510:510::/home/stu08:/bin/bash
3.6.5 过滤
[[email protected] ~]# awk ‘/stu08/‘ oldboy.log    
stu08:x:510:510::/home/stu08:/bin/bash
有些类似sed的过滤
[[email protected] ~]# sed -n ‘/stu08/p‘ oldboy.log
stu08:x:510:510::/home/stu08:/bin/bash

本文出自 “啊凯” 博客,请务必保留此出处http://kaile.blog.51cto.com/12459560/1908093

以上是关于AWK命令总结的主要内容,如果未能解决你的问题,请参考以下文章

AWK命令总结

awk 总结

shell常见命令总结

AWK:常用总结

linux 常用命令总结

shell常见命令总结