1

Sample input:

1:2:3:0:5
1:2:3::5

Awk processing:

$ echo -e "1:2:3:0:5\n1:2:3::5"|awk -F: '{if($4!="") print NR,$4; else print NR,"-"}'
1 0
2 -

Question: can above be run in a short if notation? I tried something like this but it does not return output as above.

$ echo -e "1:2:3:0:5\n1:2:3::5"|awk -F: '{print NR,($4 ? $4 : "-")}'
1 -
2 -
3

0 is evaluated as false and by condition $4 ? $4 : "-" it comes to "-".
To check for empty fourth field the condition $4 != "" should be specified.

awk -F':' '{ print NR, ($4 != ""? $4 : "-") }' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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