如何知道 awk 命令是否在不破坏原始输出的情况下打印了一些东西

how to know if awk command printed something without spoiling original output

我正在尝试使用 AWK 命令,它的打印效果非常好,我想要的确切方式

我的问题是,我如何从脚本中知道我使用的 awk 命令是否打印了一些东西而没有破坏并改变了它的打印方式?

我使用的命令: gawk 'BEGIN{RS=ORS="\n\n" {s=tolower([=14=])} s~/word1|word2/' input_file.log

我试过:

status=gawk 'BEGIN{RS=ORS="\n\n" {s=tolower([=15=])} s~/word1|word2/' input_file.log

if [ -z $status]
then
    //status is empty or null, means nothing printed in awk command
    echo "nothing"
else
    //printed something in awk command
    echo $status

问题是 echo $status 按顺序打印所有行,行之间没有 "new lines"

如何才能在不损坏 awk 的情况下打印原始打印件?

示例: 输入文件:

line 0 no words in here

line 1 starting
line 1 word1

line 2 no words here as well

line 3 starting
line 3 word2
line 3 end

line 4 nothing
line 5 nothing

命令: gawk 'BEGIN{RS=ORS="\n\n" {s=tolower([=14=])} s~/word1|word2/' input_file.log

预期输出:

line 1 starting
line 1 word1

line 3 starting
line 3 word2
line 3 end

如果我使用: stat=$(gawk 'BEGIN{RS=ORS="\n\n" {s=tolower([=18=])} s~/word1|word2/' input_file.log) echo $stat

我得到输出:

line 1 starting line 1 word1 line 3 starting line 3 word2 line 3 end

提前致谢!

不完全确定,因为您没有显示任何示例 Input_file 或预期的输出,所以您可以尝试使用 echo "$status".

编辑: 由于您现在已经编辑了 post,因此您应该将代码更改为以下内容,然后它应该会飞起来。

status=$(awk 'BEGIN{RS=ORS="\n\n"} {s=tolower([=10=])} s~/word1|word2/' Input_file)
if [[ -z $status ]]
then
    echo "nothing"
else
    echo "$status"
fi

您可以使用 exit 代码来检查 awk 是否打印了某些东西

更正您的代码

来自

gawk 'BEGIN{RS=ORS="\n\n" {s=tolower([=10=])} s~/word1|word2/' input_file.log

status=$(gawk 'BEGIN{RS=ORS="\n\n"}tolower([=11=])~/word1|word2/' input_file.log)

和(带引号)

echo "$status"

This happens because when you quote a parameter (whether that parameter is passed to echo, test or some other command), the value of that parameter is sent as one value to the command. If you don't quote it, the shell does its normal magic of looking for whitespace to determine where each parameter starts and ends.

更正现有代码

#!/usr/bin/env bash

status=$(gawk 'BEGIN{RS=ORS="\n\n"}tolower([=13=])~/word1|word2/' input_file.log)
if [  -z "$status" ]; then
       echo "looks like nothing matched and so nothing printed"
else
       echo "awk matched regex and printed something"
fi

这是使用退出代码检查 awk 是否打印了某些内容的代码:

gawk 'BEGIN{RS=ORS="\n\n"}f=(tolower([=14=])~/word1|word2/){e=1}f; END{exit !e}' input_file.log

# check exit code 
if [ "$?" -eq 0 ]; then
       echo "awk matched regex and printed something"
else
       echo "looks like nothing matched and so nothing printed"
fi

测试结果:

$ cat test.sh 
#!/usr/bin/env bash

gawk 'BEGIN{RS=ORS="\n\n"}f=(tolower([=15=])~/word1|word2/){e=1}f; END{exit !e}' ""
if [ "$?" -eq 0 ]; then
       echo "awk matched regex and printed something"
else
       echo "looks like nothing matched and so nothing printed"
fi

用于测试的示例文件

$ echo 'word1' >file1

$ echo 'nothing' >file2

文件内容

$ cat file1
word1

$ cat file2
nothing

使用第一个文件执行

$ bash test.sh file1
word1

awk matched regex and printed something

用第二个文件执行

$ bash test.sh file2
looks like nothing matched and so nothing printed