Filter valid hexadecimal color codes

So I have a text file containing valid and invalid hexadecimal color codes. Would I need to build a custom c program to filter the valid color codes or would a standard unix command achieve this?

well... first, you'd need to define what makes the code "invalid".
Them look at your "text file" and try to determine the logical pattern how to make the "call".

So I think I found out a good way of doing it by using grep and regular expression to filter out the non-valid codes but it doesn't seem to work and I don't know why

grep "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})" colours.txt

I don't know either...
Most likely because you're using Extended RegEx without telling grep to do so (-E)
could you provide a sample file and a desired output (as always)?

I tried grep -E and it didn't work. I've attached the text file that I am using and I basically want to return valid codes that have a hash symbol and have 6 or 8 characters ranging from A-Z, a-z or 0-9.

grep -E "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})" colours.txt

how about to start with - you can enhance it to match ONLY 6 or 8 chars (not 6 though 8 including 7) - left as an exercise:

grep -iE '^#[a-f0-9]{6,8}$' colours.txt
2 Likes

How about - after removing the DOS line terminators (<CR> = ^M = 0x0D = \r) that sabotage any regex -

grep -E "#[[:xdigit:]]{6}([[:xdigit:]]{2})?$" colours.txt
2 Likes

I believe valid hexadecimal color codes can have 3, 4, 6, or 8 hexadecimal digits, check out 8-Digit Hex Codes? | CSS-Tricks - CSS-Tricks.
So, your grep pattern should match all of these... You can achieve this with:

grep -E "#([[:xdigit:]]{3,4}|[[:xdigit:]]{6}|[[:xdigit:]]{8})\b" colours.txt

From man re_format : \b matches the null string at a word boundary (either the beginning or end of a word).