Linux awk Command Process log text and global variables example

Source: Internet
Author: User

How to use awk

awk ' {pattern + action} ' {filenames}

Although the operation can be complex, the syntax is always the same, where pattern represents what AWK looks for in the data, and the action is a series of commands that are executed when the matching content is found. Curly braces ({}) do not need to appear in the program at all times, but they are used to group a series of instructions according to a specific pattern. pattern is the regular expression to be represented, surrounded by slashes.

AWK Global variables specific methods are:

The code is as follows Copy Code

Cat Access.log |awk ' begin{devname= ';} ...'

The ellipsis section is for awk processing code.

For example:

  code is as follows copy code
cat Tmp1.log | awk ' begin{start=0;tmp=0}{if ($ = = "Traceback") {Split ($0,b, "script  ");p rint b[2];start=1;tmp=$11;} else {if (start ==1) {if ($11 = = tmp) {split ($0,b, "script  ");p rint b[2];start=1;} else {start=0;print '}} '

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.