如果在文件中不存在字符串时,如果其他条件

发布于 2025-02-09 16:44:28 字数 2395 浏览 3 评论 0原文

我正在尝试使用awk将多行块转换为单个记录,然后尝试在其上运行搜索操作。我正在运行lspci -v作为输入命令,但是我对此问题进行了模拟数据。

输入数据:

name: foobar
data: 123 bad

name: foozoo
data: 123 good

name: foozoo
data: 123 bad

name: zoobar
data: 123 good

name: barzpp
data: 123 bad

首先,我将块中的输入数据转换为单线记录。

awk -v RS='' '{$1=$1}1' xx
name: foobar data: 123 bad
name: foozoo data: 123 good
name: foozoo data: 123 bad
name: zoobar data: 123 good
name: barzpp data: 123 bad

现在,我正在搜索一个字符串“ foozoo”,这给了我期望的结果。在这里,我首先检查了行上是否存在foozoo,然后我检查是否存在。这很好。

awk -v RS='' -v var=foozoo '{$1=$1}; {if(match($0,var)) if(match($0,var ".*good")) print var " is good"; else print  var " is missing"}' xx
foozoo is good
foozoo is missing

现在,当我提供一个不存在的字符串时,awk将什么都没有返回,这很有意义,因为没有else block。

awk -v RS='' -v var=THIS_DOES_NOT_EXIST '{$1=$1}; {if(match($0,var)) if(match($0,var ".*good")) print var " is good"; else print  var " is missing"}' xx

当我放置其他块并搜索现有的输入中的现有字符串。我明白了,我不想要这个。我只希望foozoo是好,而foozoo是不好的行。

awk -v RS='' -v var=foozoo '{$1=$1}; {if(match($0,var)) {if(match($0,var ".*good")) print var " is good"; else print  var " is missing"} else {print "NON-EXISTING_DATA_REQUESTED"}}' xx
NON-EXISTING_DATA_REQUESTED
foozoo is good
foozoo is missing
NON-EXISTING_DATA_REQUESTED
NON-EXISTING_DATA_REQUESTED

同样,当我运行不存在的数据时,我会获得行non-existing_data_requested y/code>,记录,如何仅打印一行说数据不存在。

awk -v RS='' -v var=monkistrying '{$1=$1}; {if(match($0,var)) {if(match($0,var ".*good")) print var " is good"; else print  var " is missing"} else {print "NON-EXISTING_DATA_REQUESTED"}}' xx
NON-EXISTING_DATA_REQUESTED
NON-EXISTING_DATA_REQUESTED
NON-EXISTING_DATA_REQUESTED
NON-EXISTING_DATA_REQUESTED
NON-EXISTING_DATA_REQUESTED

这是上面的最后一个脚本由gawk -o-

{
        $1 = $1
}

{
        if (match($0, var)) {
                if (match($0, var ".*good")) {
                        print var " is good"
                } else {
                        print var " is missing"
                }
        } else {
                print "NON-EXISTING_DATA_REQUESTED"
        }
}

I am trying to use awk to convert a multi-line block to a single record and then trying to run a search operation on it. I am running lspci -v as the input command, but to I have mock the data for this question.

Input data:

name: foobar
data: 123 bad

name: foozoo
data: 123 good

name: foozoo
data: 123 bad

name: zoobar
data: 123 good

name: barzpp
data: 123 bad

First I converted the input data that was in blocks into single-line records.

awk -v RS='' '{$1=$1}1' xx
name: foobar data: 123 bad
name: foozoo data: 123 good
name: foozoo data: 123 bad
name: zoobar data: 123 good
name: barzpp data: 123 bad

Now I am searching for a string "foozoo" and this gives me desired results. Here, I am first checking if foozoo is present on the line, and then I am checking if .*good is present in the same line. This works fine.

awk -v RS='' -v var=foozoo '{$1=$1}; {if(match($0,var)) if(match($0,var ".*good")) print var " is good"; else print  var " is missing"}' xx
foozoo is good
foozoo is missing

Now, when I supply a non-existing string the awk will return nothing, which make sense as there is no else block.

awk -v RS='' -v var=THIS_DOES_NOT_EXIST '{$1=$1}; {if(match($0,var)) if(match($0,var ".*good")) print var " is good"; else print  var " is missing"}' xx

When I put else block and search for an existing, string in the input. I get this, I do not want this. I only want the foozoo is good and foozoo is bad lines.

awk -v RS='' -v var=foozoo '{$1=$1}; {if(match($0,var)) {if(match($0,var ".*good")) print var " is good"; else print  var " is missing"} else {print "NON-EXISTING_DATA_REQUESTED"}}' xx
NON-EXISTING_DATA_REQUESTED
foozoo is good
foozoo is missing
NON-EXISTING_DATA_REQUESTED
NON-EXISTING_DATA_REQUESTED

Similarly, when I run for non-existing data, I get the line NON-EXISTING_DATA_REQUESTED for each, record, how to print just one line saying data does not exist.

awk -v RS='' -v var=monkistrying '{$1=$1}; {if(match($0,var)) {if(match($0,var ".*good")) print var " is good"; else print  var " is missing"} else {print "NON-EXISTING_DATA_REQUESTED"}}' xx
NON-EXISTING_DATA_REQUESTED
NON-EXISTING_DATA_REQUESTED
NON-EXISTING_DATA_REQUESTED
NON-EXISTING_DATA_REQUESTED
NON-EXISTING_DATA_REQUESTED

Here's that last script above formatted legibly by gawk -o-:

{
        $1 = $1
}

{
        if (match($0, var)) {
                if (match($0, var ".*good")) {
                        print var " is good"
                } else {
                        print var " is missing"
                }
        } else {
                print "NON-EXISTING_DATA_REQUESTED"
        }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月光色 2025-02-16 16:44:28

在我看来,您只想打印non-existing_data_requested 如果没有匹配(foozoogood)找到,然后仅打印一次出现non-existing_data_requested;如果这是正确的,一个想法是跟踪匹配的数量,在结束{...} block中,如果计数为零,则打印单一的出现non--现有_data_requested ...

找到匹配:

awk -v RS='' -v var=foozoo '
    { $1=$1 }
    { if(match($0,var)) {
        # found++                              # uncomment if both "is good" AND "is missing" should be considered as "found"
        if(match($0,var ".*good"))
          { print var " is good"; found++ }    # remove "found++" if the previous line is uncommented
        else 
          { print var " is missing"       }
      }
    }
END { if (!found) print "NON-EXISTING_DATA_REQUESTED" }
' xx

foozoo is good
foozoo is missing

没有匹配:

awk -v RS='' -v var=monkistrying  '
    { $1=$1 }
    { if(match($0,var)) {
        # found++
        if(match($0,var ".*good")) 
          { print var " is good"; found++ }
        else
          { print var " is missing"       }
      }
    }
END { if (!found) print "NON-EXISTING_DATA_REQUESTED" }
' xx

NON-EXISTING_DATA_REQUESTED

It sounds to me that you only want to print NON-EXISTING_DATA_REQUESTED if no matches (foozoo and good) are found and then only print one occurrence of NON-EXISTING_DATA_REQUESTED; if this is correct, one idea would be to keep track of the number of matches and in an END{...} block if that count is zero then print the single occurrence of NON-EXISTING_DATA_REQUESTED ...

Found a match:

awk -v RS='' -v var=foozoo '
    { $1=$1 }
    { if(match($0,var)) {
        # found++                              # uncomment if both "is good" AND "is missing" should be considered as "found"
        if(match($0,var ".*good"))
          { print var " is good"; found++ }    # remove "found++" if the previous line is uncommented
        else 
          { print var " is missing"       }
      }
    }
END { if (!found) print "NON-EXISTING_DATA_REQUESTED" }
' xx

foozoo is good
foozoo is missing

Found no matches:

awk -v RS='' -v var=monkistrying  '
    { $1=$1 }
    { if(match($0,var)) {
        # found++
        if(match($0,var ".*good")) 
          { print var " is good"; found++ }
        else
          { print var " is missing"       }
      }
    }
END { if (!found) print "NON-EXISTING_DATA_REQUESTED" }
' xx

NON-EXISTING_DATA_REQUESTED
贵在坚持 2025-02-16 16:44:28

无需将您的记录压缩到各个行上,这只是浪费时间,并可能使比较更加困难,并且通过使用match()您正在将var作为Regexp当您看起来只需要进行全场比较时,进行部分记录比较。尝试匹配($ 0,var)当输入包含badfoozooherefoozoo给定-v var = var = foozoo以查看您使用match()的一种方式将失败(还有其他几个)。另外,由于您不使用rstart或rllength,因此使用match($ 0,var)而不是$ 0〜 var无论如何还是效率低下。

$ cat tst.awk
BEGIN { RS="" }
$2 == var {
    print var, "is", ( $NF == "good" ? "good" : "missing" )
    found = 1
}
END {
    if ( !found ) {
        print "NON-EXISTING_DATA_REQUESTED"
    }
}

$ awk -v var='foozoo' -f tst.awk file
foozoo is good
foozoo is missing

$ awk -v var='monkistrying' -f tst.awk file
NON-EXISTING_DATA_REQUESTED

There's no need to compress your records onto individual lines, that's just wasting time and potentially making the comparisons harder, and by using match() you're treating var as a regexp and doing a partial record comparison when it looks like you just want a string full-field comparison. Try match($0,var) when the input contains badfoozoohere and foozoo given -v var=foozoo to see one way in which the way you're using match() will fail (there are several others). Also since you aren't using RSTART or RLENGTH, using match($0,var) instead of $0 ~ var was inefficient anyway.

$ cat tst.awk
BEGIN { RS="" }
$2 == var {
    print var, "is", ( $NF == "good" ? "good" : "missing" )
    found = 1
}
END {
    if ( !found ) {
        print "NON-EXISTING_DATA_REQUESTED"
    }
}

$ awk -v var='foozoo' -f tst.awk file
foozoo is good
foozoo is missing

$ awk -v var='monkistrying' -f tst.awk file
NON-EXISTING_DATA_REQUESTED
划一舟意中人 2025-02-16 16:44:28

单个通用awk基于解决的解决方案,不需要转换数据:

  • bytes xc0 \ 300 \ 300xc1 \ 301和<代码> xf9 \ 371 不是utf-8有效,

    因此,它们出现在输入数据中的机会绝对很小

输入

name: foobar
data: 123 bad

name: foozoo
data: 123 good

name: foozoo
data: 123 bad

name: zoobar
data: 123 good

name: barzpp
data: 123 bad

代码gawkmawk 1/2,或<代码> lc_all = c nawk )

{m,n~,g}awk '
 BEGIN {
     ______ = "NON-EXISTING_DATA_REQUESTED\n"
         FS = "((data|name): ([0-9]+ )?|\n)+"
         RS = "^$" (ORS = _)
        ___ = "\300"
       ____ =  "\371"
      _____ =(_="\301")(__="foozoo")(\
        OFS = _)
 } ! (  NF *= /name: foozoo[ \n]/) ? $NF = ______\
    : gsub(_____  "bad"_, (_)(___)_) +            \
      gsub(_____ "good"_,(_)(____)_) + gsub("[\1-~]+","")+\
      gsub( ___, __ " is missing\n") +                     \
      gsub(____, __ " is " "good\n") + gsub((___)"|"(_)("|")____,"")'

输出

foozoo is good
foozoo is missing

single-pass awk based solution w/o needing to transform the data:

  • bytes xC0 \300, xC1 \301, and xF9 \371 aren't UTF-8 valid,

    so chances of them appearing in input data are absolutely minuscule

INPUT

name: foobar
data: 123 bad

name: foozoo
data: 123 good

name: foozoo
data: 123 bad

name: zoobar
data: 123 good

name: barzpp
data: 123 bad

CODE (gawk, mawk 1/2, or LC_ALL=C nawk)

{m,n~,g}awk '
 BEGIN {
     ______ = "NON-EXISTING_DATA_REQUESTED\n"
         FS = "((data|name): ([0-9]+ )?|\n)+"
         RS = "^
quot; (ORS = _)
        ___ = "\300"
       ____ =  "\371"
      _____ =(_="\301")(__="foozoo")(\
        OFS = _)
 } ! (  NF *= /name: foozoo[ \n]/) ? $NF = ______\
    : gsub(_____  "bad"_, (_)(___)_) +            \
      gsub(_____ "good"_,(_)(____)_) + gsub("[\1-~]+","")+\
      gsub( ___, __ " is missing\n") +                     \
      gsub(____, __ " is " "good\n") + gsub((___)"|"(_)("|")____,"")'

OUTPUT

foozoo is good
foozoo is missing
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文