代码之家  ›  专栏  ›  技术社区  ›  Saeed isa

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

  •  1
  • Saeed isa  · 技术社区  · 8 年前

    我正在尝试使用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
    

    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
    

    提前感谢!

    2 回复  |  直到 8 年前
        1
  •  2
  •   RavinderSingh13 Nikita Bakshi    8 年前

    不完全确定,因为您没有显示任何示例Input_文件或预期输出,所以请尝试使用 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
  •  1
  •   Akshay Hegde    8 年前

    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"
    

    这是因为 当您引用参数时(是否 echo , test 或其他命令),则 它,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