Awk not equal

Did I do something wrong with this awk not equal? For some reason it prints twice.

>awk '{if ($4 != "root") print $1 " " $4 " " $5}' ls_test
server10: njs nodeadm
server10: njs nodeadm
>grep server10 ls_test
server10: drwxr-sr-x. 18 njs nodeadm 4096 Aug 16 09:42 /opt
>

Try this:

awk ' $4 != "root" { print $1 " " $4 " " $5}
                                {next} ' ls_test
server10: njs nodeadm

The default for awk is to print what it reads in.
bash shell does something similar:

# set positional variables
$ set $( cat ls_test)
# if shell variable not equal to "roor"  print stuff
$[ $4 != "root" ] && echo "$1 $4  $%"
server10: njs  $%

No, the awk default is to print the whole line if there is a true condition outside an { action } and not followed by an { action }.
Post#1 should work as intended.

I rather suspect that the input file's last line is not newline-terminated.
Some tools handle it, some ignore it.

I see that more and more Java classes produce such non-compiant text files.

2 Likes
Moderator comments were removed during original forum migration.

This topic was automatically closed 29 days after the last reply. New replies are no longer allowed.