Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.