【问题标题】:how to know if awk command printed something without spoiling original output如何知道 awk 命令是否在不破坏原始输出的情况下打印了某些内容
【发布时间】:2017-09-24 10:38:48
【问题描述】:

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

我的问题是,我如何从脚本中知道我使用的 awk 命令是否在不破坏和改变打印方式的情况下打印了某些内容?

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

我试过了:

status=gawk 'BEGIN{RS=ORS="\n\n" {s=tolower($0)} 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 按顺序打印所有行,行之间没有“新行”

如何从 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($0)} 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($0)} s~/word1|word2/' input_file.log) echo $stat

我得到输出:

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

提前致谢!

【问题讨论】:

  • 1) 如果您设置 RS="" 而不是 RS="\n\n" 您的脚本将工作相同但不是特定于 gawk 的,2) 没有单词边界,您将在查找时匹配 foobar foo, 3) shell cmets 以# 开头,而不是//

标签: bash gawk awk


【解决方案1】:

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

编辑:由于您现在已经编辑了您的帖子,所以您应该将您的代码更改为以下代码,然后它应该会飞起来。

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

【讨论】:

  • 很高兴它对您有所帮助,请确保您也进行了我在帖子中提到的那些必要的更改(修复了代码中的一些语法问题)。如果有任何疑问,请告诉我。
【解决方案2】:

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

更正您的代码

来自

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

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

和(带引号)

echo "$status"

发生这种情况是因为当你引用一个参数时(无论是 参数传递给echotest 或其他命令),值 该参数作为一个值发送给命令。如果你不报价 它,shell 执行其寻找空格的正常魔法 确定每个参数的开始和结束位置。

更正现有代码

#!/usr/bin/env bash

status=$(gawk 'BEGIN{RS=ORS="\n\n"}tolower($0)~/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($0)~/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($0)~/word1|word2/){e=1}f; END{exit !e}' "$1"
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

【讨论】:

  • 谢谢!使用引号,感谢您解释引号的事情,之前不知道(Y)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 2021-10-08
  • 2015-08-19
  • 2021-12-31
  • 1970-01-01
  • 2017-08-10
相关资源
最近更新 更多