Match output fields agains two patterns

I need to print field and the next one if field matches 'patternA' and also print 'patternB' fields.

echo "some output" | awk '{for(i=1;i<=NF;i++){if($i ~ /patternA/){print $i, $(i+1)}elif($i ~ /patternB/){print $i}}}'

This code returnes me 'syntax error'. Pls advise how to do properly.

In the future, please always tell us what operating system and shell you're using when you start a thread in the Shell Programming and Scripting forum, and, when you get a syntax error, please show us the actual diagnostic message that you get when you try running your code.

In this case, I would guess that your problem is that standard implementations of awk do not include an elif clause in an if statement. If you change the elif in this case to else if :

echo "some output" |
awk '{	for(i=1;i<=NF;i++){
		if($i ~ /patternA/){
			print $i, $(i+1)
		} else	if($i ~ /patternB/){
				print $i
			}
	}
}'

you might get what you want, although nothing will be printed by your sample code given the input you are providing to your awk script.

With an unusual record separator (the defailt FS, i.e. space, <TAB>, <new line>, the solution might be simplified:

echo "some output" | awk -vRS="[ 	
]" '
/PatternA/ {X = $0
            getline
            print X, $0
           }
/PatternB/
'