linux--awk
Posted lxsjl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux--awk相关的知识,希望对你有一定的参考价值。
1、打印每行的最后一列
awk -F "[ ]+" ‘{print $NF}‘
2、打印第一行最后一列
awk -F "[ ]+" ‘NR=1{print $NF}‘
3、打印每行的倒数第二列
awk ‘{print $(NF-1)}‘ filename
4、awk -F ‘[: ]+‘ ‘/eth1/{print $3}‘
//-F[: ]表示指定分隔符可以是:或者 (空格) 这里指定多个分隔符
[ ] 表示一个字符的集合,+则是一个正则表达式,表示+前面的字符(:或者空格)重复1次或者一次以上
echo "1::::2 3 4" | awk -F ‘[: ]+‘ ‘{print $4}‘ 4 echo "1::::2 3 4" | awk -F ‘[: ]+‘ ‘{print $1}‘ 1 echo "1::::2 3 4" | awk -F ‘[: ]+‘ ‘{print $2}‘ 2 echo "1::::2 3 4" | awk -F ‘[: ]+‘ ‘{print $3}‘ 3 无空格 [email protected]:~$ echo "1::::2 3 4" | awk -F ‘[:]+‘ ‘{print $2}‘ 2 3 4 [email protected]:~$ echo "1::::2 3 4" | awk -F ‘[:]+‘ ‘{print $1}‘ 1 无冒号 [email protected]:~$ echo "1::::2 3 4" | awk -F ‘[ ]+‘ ‘{print $2}‘ 3 [email protected]:~$ echo "1::::2 3 4" | awk -F ‘[ ]+‘ ‘{print $1}‘ 1::::2 无加号(正则) [email protected]:~$ echo "1::::2 3 4" | awk -F ‘[ ]‘ ‘{print $1}‘ 1::::2 [email protected]:~$ echo "1::::2 3 4" | awk -F ‘[ ]‘ ‘{print $2}‘ 3 [email protected]:~$ echo "1::::2 3 4" | awk -F ‘[ ]‘ ‘{print $3}‘ 4 无加号,以:空格分割 [email protected]:~$ echo "1::::2 3 4" | awk -F ‘[: ]‘ ‘{print $1}‘ 1 [email protected]:~$ echo "1::::2 3 4" | awk -F ‘[: ]‘ ‘{print $2}‘ [email protected]:~$ echo "1::::2 3 4" | awk -F ‘[: ]‘ ‘{print $3}‘ [email protected]:~$ echo "1::::2 3 4" | awk -F ‘[: ]‘ ‘{print $4}‘ [email protected]:~$ echo "1::::2 3 4" | awk -F ‘[: ]‘ ‘{print $5}‘ 2 [email protected]:~$ echo "1::::2 3 4" | awk -F ‘[: ]‘ ‘{print $6}‘ 3 [email protected]:~$ echo "1::::2 3 4" | awk -F ‘[: ]‘ ‘{print $7}‘ 4 [email protected]:~$ echo "1::::2 3 4" | awk -F ‘[: ]‘ ‘{print $8}‘ [email protected]:~$ echo "1::::2 3 4" | awk -F ‘[: ]‘ ‘{print $9}‘ [email protected]:~$
以上是关于linux--awk的主要内容,如果未能解决你的问题,请参考以下文章