本文介绍了如何在awk中的模式上进行if if else匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试了以下命令:awk'/search-pattern/{print $ 1}'如何为上述命令编写else部分?
i tried the below command:awk '/search-pattern/ {print $1}'How do i write the else part for the above command ?
推荐答案
awk '{if ($0 ~ /pattern/) {then_actions} else {else_actions}}' file
$0
代表整个输入记录.
另一种惯用方式基于三元运算符语法selector ? if-true-exp : if-false-exp
Another idiomatic waybased on the ternary operator syntax selector ? if-true-exp : if-false-exp
awk '{print ($0 ~ /pattern/)?text_for_true:text_for_false}'
awk '{x == y ? a[i++] : b[i++]}'
awk '{print ($0 ~ /two/)?NR "yes":NR "No"}' <<<$'one two\nthree four\nfive six\nseven two'
1yes
2No
3No
4yes
这篇关于如何在awk中的模式上进行if if else匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!