如果在文件中不存在字符串时,如果其他条件
我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在我看来,您只想打印
non-existing_data_requested
如果没有匹配(foozoo
和good
)找到和,然后仅打印一次出现non-existing_data_requested
;如果这是正确的,一个想法是跟踪匹配的数量,在结束{...}
block中,如果计数为零,则打印单一的出现non--现有_data_requested
...找到匹配:
没有匹配:
It sounds to me that you only want to print
NON-EXISTING_DATA_REQUESTED
if no matches (foozoo
andgood
) are found and then only print one occurrence ofNON-EXISTING_DATA_REQUESTED
; if this is correct, one idea would be to keep track of the number of matches and in anEND{...}
block if that count is zero then print the single occurrence ofNON-EXISTING_DATA_REQUESTED
...Found a match:
Found no matches:
无需将您的记录压缩到各个行上,这只是浪费时间,并可能使比较更加困难,并且通过使用
match()
您正在将var
作为Regexp当您看起来只需要进行全场比较时,进行部分记录比较。尝试匹配($ 0,var)
当输入包含badfoozoohere
和foozoo
给定-v var = var = foozoo
以查看您使用match()
的一种方式将失败(还有其他几个)。另外,由于您不使用rstart或rllength,因此使用match($ 0,var)
而不是$ 0〜 var
无论如何还是效率低下。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 treatingvar
as a regexp and doing a partial record comparison when it looks like you just want a string full-field comparison. Trymatch($0,var)
when the input containsbadfoozoohere
andfoozoo
given-v var=foozoo
to see one way in which the way you're usingmatch()
will fail (there are several others). Also since you aren't using RSTART or RLENGTH, usingmatch($0,var)
instead of$0 ~ var
was inefficient anyway.单个通用
awk
基于解决的解决方案,不需要转换数据:bytes
xc0 \ 300 \ 300
,xc1 \ 301
和<代码> xf9 \ 371 不是utf-8
有效,因此,它们出现在输入数据中的机会绝对很小
输入
代码(
gawk
,mawk 1/2
,或<代码> lc_all = c nawk )输出
single-pass
awk
based solution w/o needing to transform the data:bytes
xC0 \300
,xC1 \301
, andxF9 \371
aren'tUTF-8
valid,so chances of them appearing in input data are absolutely minuscule
INPUT
CODE (
gawk
,mawk 1/2
, orLC_ALL=C nawk
)OUTPUT