Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have an example:

echo $(seq 9) | awk '{for(i=1;i<=NF;i++) if(i!=5&&i!=6) {printf "%s ",i}else{printf "%s ",""} print ""}'
1 2 3 4   7 8 9 

Is it possible to use the shorthand of this command?

echo $(seq 9)| awk '{for(i=1;i<=NF;i++) (i!=5&&i!=6) ? {printf "%s ",i} : {printf "%s ",""} print ""}'

Thank you for the explanation.

share|improve this question
up vote 5 down vote accepted

Possible, but not that way. You have to supply expressions to the ternary operator. In AWK printf is a statement, so you can not use it there, like you can neither have {} either.

As you not need two printf statements anyway, better write it like this:

seq -s ' ' 9 | awk '{for(i=1;i<=NF;i++) printf "%s ", i!=5&&i!=6 ? i : ""; print ""}'
share|improve this answer
Thank you for a good solution. – nowy1 Dec 16 '12 at 17:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.