How to do a if else match on pattern in awk
问题 i tried the below command: awk '/search-pattern/ {print $1}' How do i write the else part for the above command ? 回答1: Classic way: awk '{if ($0 ~ /pattern/) {then_actions} else {else_actions}}' file $0 represents the whole input record. 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