=Start=
Linux的cut命令学习
参考链接:
- http://www.computerhope.com/unix/ucut.htm #非常全
- http://www.thegeekstuff.com/2013/06/cut-command-examples/ #非常全
- http://www.yourownlinux.com/2015/05/cut-command-in-linux-tutorial-with-examples.html
- http://unix.stackexchange.com/questions/122055/what-constitudes-a-field-for-the-cut-command
- http://stackoverflow.com/questions/22727107/how-to-find-the-last-field-using-cut-linux
- http://stackoverflow.com/questions/4956873/cutting-first-n-and-last-n-columns
- http://bencane.com/2012/10/22/cheat-sheet-cutting-text-with-cut/
- http://stackoverflow.com/questions/15353655/how-to-use-cut-command-in-bash-to-show-all-columns-except-those-indicated
- http://blog.51yip.com/linux/1077.html
- http://www.unixlore.net/articles/the-forgotten-power-of-unix-text-utilities.html
参考解答:
cut命令的 --complement 选项 if we use --complement as an option, cut command will display all the fields, but the specified field. {使用 --complement选项 打印除了指定列外的所有列}{complement n.补充; 补足语; 补充物; 补集(数);}
==
$ echo a b c a b c $ echo a b c | cut -f 1 -d ' ' a $ echo a b c | cut -f 1,2 -d ' ' a b $ echo a b c | cut -f 1 -d ' ' --complement b c $ echo a b c | cut -d ' ' -f 2- b c
用awk打印除了第一列/最后一列的其他列
搜索关键字:
- Linux awk print except first column
参考链接:
- http://stackoverflow.com/questions/4198138/printing-everything-except-the-first-field-with-awk
- http://stackoverflow.com/questions/2961635/using-awk-to-print-all-columns-from-the-nth-to-the-last
- http://stackoverflow.com/questions/3046291/awk-need-to-print-everything-all-rest-fields-except-1-and-2
- http://www.theunixcode.com/2013/12/using-awk-to-print-all-columns-from-the-nth-to-the-last/
- http://www.cyberciti.biz/faq/unix-linux-bsd-appleosx-skip-fields-command/
- http://onetipperday.blogspot.hk/2012/02/print-all-columns-except-last-one.html
参考解答:
##打印除了第一列外的所有列## awk '{$1=""; print $0}' input.txt # Will leave a leading space awk '{for(i=2; i<=NF; i++) print $i}' input.txt cut -d ' ' -f 1 --complement input.txt cut -d ' ' -f 2- input.txt ##打印除了最后一列外的所有列## awk '{$NF=""; print $0}' input.txt awk '{for(i=1; i<NF; i++) print $i}' input.txt rev input.txt | cut -f2- | rev
=EOF=
《 “Linux的awk/cut命令学习” 》 有 5 条评论
linux awk 内置函数详细介绍(实例)
http://www.cnblogs.com/chengmo/archive/2010/10/08/1845913.html
awk内置字符串函数详解
https://www.linuxsong.org/2010/09/awk-string-function/
https://stackoverflow.com/questions/8009664/how-to-split-a-delimited-string-into-an-array-in-awk
`
#需要把一些无用的字符「双引号、单引号、空格等」去掉
$ head input.csv | awk -F’,’ ‘NR>7 {print $8, $10, $11}’
‘9RCED3 “张三 zhangsan 公司A”
’98YTC “李四 lisi”
‘ixyzero “王五 zero.wang”
$ head input.csv | awk -F’,’ ‘NR>7 {print NF”\t”$11}’
$ head input.csv | awk -F’,’ ‘NR>7 {split($11,mis,” “); print substr($8,2,15), substr($10,2,15), mis[0]}’
$ head input.csv | awk -F’,’ ‘NR>7 {split($11,mis,” “);sub(“\””, “”, mis[1]); print substr($8,2,15), substr($10,2,15), mis[1]}’
9RCED3 张三 zhangsan
98YTC 李四 lisi
ixyzero 王五 zero.wang
awk的函数要放在前面,不能直接放在 print 语句里面,否则打印的是函数的返回值(0或1):
split()
substr()
`
看示例学 awk
https://toutiao.io/posts/eozd8t/preview
https://mp.weixin.qq.com/s/6YVC9hA2fMEjclWmseevXA
`
awk使用经验:
如果处理逻辑复杂,就将代码写进文件里,再通过 -f 引用;这样可防止敲错代码。
灵活调用系统命令,内置变量,使代码更简洁。
处理大文件时,多审核优化逻辑代码,可有效提高执行效率。
`
高逼格|shell一出手,分析服务器日志不用愁!
https://mp.weixin.qq.com/s/JdPqrU6Jgc4AiCg9KTbUEw
https://segmentfault.com/a/1190000009745139
Linux生产环境上,最常用的一套“AWK“技巧
https://mp.weixin.qq.com/s/aRy3QlMUpSNOKf2pyN6Uuw
`
打印某一列
网络状态统计(去重计数)
注意点
1、awk的主程序部分使用单引号‘包围,而不能是双引号
2、awk的列开始的index是0,而不是1
`
awk中如何判断某列是否包含关键字?
https://stackoverflow.com/questions/17001849/awk-partly-string-match-if-column-word-partly-matches
https://www.unix.com/unix-for-advanced-and-expert-users/156232-awk-line-contains-someting-field.html
`
# $4 包含 John
awk ‘$4 ~ “John” {print}’ myFile
awk ‘$4 ~ /John/ {print}’ myFile
# 使用 index 函数
awk ‘index($4, “John”)’ myFile
# $4 等于 John
awk ‘$4 == “John” {print}’ myFile
`