Awk to find space and tab.

Wants to print line when there exist leading or trailing space or tab in fields 2,3 and 5

The below code prints all lines in file even if they dont have leading and trailing space or tab.

nawk -F"|" '{for(i=1;i<=NF;i++) {if ($i ~ "^[ \t]*" || $i ~ "[ \t]*$")}}1' file

file

Ouput required:

Thanks

Change the * to +. You have made the extremely common mistake of using an REGEX with a zero length match.

 nawk -F"|" '{for(i=1;i<=NF;i++) {if ($i ~ "^[ \t]+" || $i ~ "[ \t]+$")}}1' file

still the same thing.
And i want to look 2,3 and 5th field only not all the fields

Yes, but you also have a '1' there, so you are printing every line. You need to delete that too, any you do nothing in the for loop/if statement.

Just check the field number in the for lop and print out the lines you want.

I removed 1 and now i want to check for leading and trailing space or tab on each field.
The below code returns nothing.

nawk -F"|" '{for(i=1;i<=NF;i++) {if ($i ~ "^[ \t]+" || $i ~ "[ \t]+$")}}' file

Help appreciated

I got the following code to work
But would like to remove sort -u and handle it with in awk

nawk -F"|" '{for(i=1;i<=NF;i++) {if ($i ~ "^[ \t]+" || $i ~ "[ \t]+$") print $0}}' file | 
sort -u  > outfile