如何在awk中的模式上进行if if else匹配 [英] How to do a if else match on pattern in awk

查看:1435
本文介绍了如何在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 ?

推荐答案

$0代表整个输入记录.

另一种惯用方式 基于三元运算符语法selector ? if-true-exp : if-false-exp

Another idiomatic way based 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匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆