awk script if or code example

Example 1: awk if else statement

# Basic syntax:
awk 'BEGIN {if(condition) outcome}' # if
awk 'BEGIN {if(condition_1) outcome_1; else outcome_2}' # if else
awk 'BEGIN {if(condition_1) outcome_1; else if(condition_2) outcome_2}' # if else if

# Example usage:
awk 'BEGIN {if(2>3) print "1_is_true"; else print 6}'
--> 6

# For multi-line and ternary operator syntax, see source

Example 2: awk or statement

# Basic syntax:
||

# Example usage:
awk '{if ($2=="abc" || $2=="def") print "true"}' input_file
# I.e. if the second field equals "abc" OR "def", awk with print "true"