Match strings with certain number of unique characters in bash

Viewed 216

I need to delete all the strings in a file that have less than 4 unique characters in them

Input:

hello
cabby
pabba
lokka
lappa
coool
apple

Expected Output:

hello
cabby
lokka
apple

I tried to think up a regular expression to do this but I can't think how it would even be possible. I did find a sed command that seems promising, it deletes all duplicate characters. However, I am not sure how to program sed to test if the program returns 4 characters, and then if it does, match the original string. sed ':1;s/\(\(.\).*\)\2/\1/g;t'

2 Answers

Using gnu awk:

awk 'BEGIN{FS=""} {
unq=0; delete seen; for (i=1; i<=NF; i++) if (!seen[$i]++) unq++} unq > 3' file

hello
cabby
lokka
apple

FS="" breaks each character into a separate field in awk.

You tried sed ':1;s/\(\(.\).*\)\2/\1/g;t', please replace t by t1.
Before your command, copy the current line in the Hold space.
After your command, replace lines with at least 4 characters left with the original line.
Now make sure you only print lines with at least four characters.

echo 'hello
cabby
pabba
lokka
lappa
coool
apple' | sed -nE 'h;:1;s/(.)(.*)\1/\1\2/g;t1;/.{4}/x;/.{4}/p'
Related