How to add output “non_assigned” when there is no match in grep?

后端 未结 1 934
青春惊慌失措
青春惊慌失措 2021-01-25 19:34

When I run a command (COMMAND) on one line of my input file (input.txt) I get an associated result where only one line is interesting, always starting by the world phylum.

<
相关标签:
1条回答
  • 2021-01-25 19:59

    This will do the job:

    for p in $(<input.txt)
    do
        (COMMAND $p | grep -w "phylum") || echo "non_assigned"
    done >> results.txt
    

    This runs the command and the grep in a subshell. If the grep finds 'phylum', the RHS of the || is not run; if 'phylum' is not found, the echo is executed to output a suitable marker.

    Notice that the redirection has been moved to after the done; it might be better to use > rather than >> now.

    Beware the set option -o pipefail — if the command can fail, you might need to use set +o pipefail. However, it is disabled by default, so you'll probably be OK.

    0 讨论(0)
提交回复
热议问题