awk multiple Separators

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.