In general, in the process of using awk, the delimiter can use the default space (which can be omitted by default) and the way the delimiter is specified.
Default format: awk ' {print $n} ' filename #n为字段值
Example: Echo "0 1 2 3 4 5 6" |awk ' {print $} ' 1
Specify the format of the delimiter: Awk-f ': ' {print $n} ' filename
Or
Awk-f: ' {print $n} ' filename #如按: Split,
such as: Awk-f ': ' {print $ '/etc/passwdawk-f: ' {print $} '/etc/passwd
Sometimes we may have multiple kinds of separators to fetch data in the process of application, what should we do?
You can use the-f ' [] ' to enclose the delimiter in brackets or the-F ' delimiter 1| delimiter 2 ' to intercept the data field:
such as: #下面就可以按空格和冒号分隔, take out the data we need echo "1:a 2:b 3:c 4:d" |awk-f ' [:] ' {print $2,$4,$6,$8} ' a b c Decho "1:a 2:b 3:c 4:d" |awk-f ' |: ' {print $2,$4,$6,$8} ' a b c D
If there is a continuous delimiter, then we will error when we take the data
such as: #我们在a后面再添加4个空格, the result will be wrong, echo "1:a 2:b 3:c 4:d" |awk-f ' [:] ' {print $2,$4,$6,$8} ' a B
The solution to this problem is the-f ' []+ ', which uses the + sign to treat the consecutive delimiters as one
echo "1:a 2:b 3:c 4:d" |awk-f ' [:]+ ' {print $2,$4,$6,$8} ' a b c D
#像下面这个可以直接取出网站外部链接过来导致的FIN_WAIT2的ipnetstat-antp|grep "Fin_wait2" |awk-f ' [:]+ ' {print $6} '
This article is from the "Operation Utopia" blog, please make sure to keep this source http://joeyang.blog.51cto.com/9092193/1675643
awk multiple Separators