remove row if string is same as previous row

I have data like:

Blue Apple 6
Red Apple 7
Yellow Apple 8
Green Banana 2
Purple Banana 8
Orange Pear 11

What I want to do is if $2 in a row is the same as $2 in the previous row remove that row. An identical $2 may exist more than one time.

So the out file would look like:

Blue Apple 6
Green Banana 2
Orange Pear 11

I'm new and don't really have any idea about the type of code that would work or if this would require something like Perl.

Thank you for your help.

awk 'NR==1{x=$2;print;next}{if (x==$2){next} else {x=$2;print}}' infile
1 Like

Here you go:

$ cat bla.txt
Blue Apple 6
Red Apple 7
Yellow Apple 8
Green Banana 2
Purple Banana 8
Orange Pear 11
$ awk '$2 != prev{print; prev=$2;}' bla.txt
Blue Apple 6
Green Banana 2
Orange Pear 11

Thanks so much all it looks great. :b:

awk '!a[$2]++' file