简体   繁体   English

需要awk命令解释

[英]Need of awk command explaination

I want to know how the below command is working.我想知道下面的命令是如何工作的。

awk '/Conditional jump or move depends on uninitialised value/ {block=1} block {str=str sep $0; sep=RS} /^==.*== $/ {block=0; if (str!~/oracle/ && str!~/OCI/ && str!~/tuxedo1222/ &&  str!~/vprintf/ && str!~/vfprintf/ && str!~/vtrace/) { if (str!~/^$/){print str}} str=sep=""}' file_name.txt >> CondJump_val.txt

I'd also like to know how to check the texts Oracle, OCI, and so on from the second line only.我还想知道如何仅从第二行检查文本 Oracle、OCI 等。

The first step is to write it so it's easier to read第一步是编写它以便于阅读

awk '
    /Conditional jump or move depends on uninitialised value/ {block=1}
    block {
        str=str sep $0
        sep=RS
    }
    /^==.*== $/ {
        block=0
        if (str!~/oracle/ && str!~/OCI/ && str!~/tuxedo1222/ &&  str!~/vprintf/ && str!~/vfprintf/ && str!~/vtrace/) {
            if (str!~/^$/) {
                print str
            }
        }
        str=sep=""
    }
' file_name.txt >> CondJump_val.txt

It accumulates the lines starting with "Conditional jump..." ending with "==...== " into a variable str.它将以“==...==”结尾的“条件跳转...”开头的行累积到变量 str 中。 If the accumulated string does not match several patterns, the string is printed.如果累积的字符串不匹配多个模式,则打印该字符串。

I'd also like to know how to check the texts Oracle, OCI, and so on from the second line only.我还想知道如何仅从第二行检查文本 Oracle、OCI 等。

What does that mean?这意味着什么? I assume you don't want to see the "Conditional jump..." line in the output. If that's the case then use the next command to jump to the next line of input.我假设您不想在 output 中看到“条件跳转...”行。如果是这种情况,则使用next命令跳转到下一行输入。

    /Conditional jump or move depends on uninitialised value/ {
        block=1
        next
    }

perhaps consolidate those regex into a single chain?也许将这些regex合并到一个链中?

 if (str?~ "oracle|OCI|tuxedo1222|v[f]?printf|vtrace") { print str }

There are two idiomatic awkisms to understand.有两个惯用的 awkism 需要理解。

The first can be simplified to this:第一个可以简化为:

$ seq 100 | awk '/^22$/{flag=1} 
/^31$/{flag=0} 
flag' 
22
23
...
30

Why does this work?为什么这行得通? In awk, flag can be tested even if not yet defined which is what the stand alone flag is doing - the input is only printed if flag is true and flag=1 is only executed when after the regex /^22$/ .在 awk 中,即使尚未定义flag也可以进行测试,这就是独立flag正在做的事情——仅当flag为真时才打印输入,并且仅在正则表达式/^22$/之后执行flag=1 The condition of flag being true ends with the regex /^31$/ in this simple example.在这个简单的例子中, flag为真的条件以正则表达式/^31$/结束。

This is an idiom in awk to executed code between two regex matches on different lines.这是awk中的一个习语,用于在不同行的两个正则表达式匹配之间执行代码。

In your case, the two regex's are:在您的情况下,两个正则表达式是:

/Conditional jump or move depends on uninitialised value/  # start
# in-between, block is true and collect the input into str separated by RS
/^==.*== $/   # end

The other 'awkism' is this:另一个“awkism”是这样的:

block {str=str sep $0; sep=RS}

When block is true, collect $0 into str and first time though, RS should not be added in-between the last time.block为真时,将$0收集到str中,但是第一次,不应在最后一次之间添加RS The result is:结果是:

str="first lineRSsecond lineRSthird lineRS..."

both depend on awk being able to use a undefined variable without error两者都依赖于 awk 能够无错误地使用未定义的变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM