Filter valid hexadecimal color codes


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Filter valid hexadecimal color codes
# 1  
Old 04-20-2020
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?
# 2  
Old 04-20-2020
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".
# 3  
Old 04-20-2020
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

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

# 4  
Old 04-20-2020
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)?
# 5  
Old 04-20-2020
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.

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

# 6  
Old 04-20-2020
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:
Code:
grep -iE '^#[a-f0-9]{6,8}$' colours.txt

This User Gave Thanks to vgersh99 For This Post:
# 7  
Old 04-20-2020
How about - after removing the DOS line terminators (<CR> = ^M = 0x0D = \r) that sabotage any regex -

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

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Filter Data based on codes

Hello, I have a question on how to filter the data on multiple columns. The problem is I have one table with 25 columns, 4500 rows. I need to filter out the data based on some codes like 'XXXX', I have 25 codes to check on that table. My requirement is that which ever row has this code I... (1 Reply)
Discussion started by: sandeep309
1 Replies

2. Shell Programming and Scripting

Help with awk color codes based on condition

HI i have two files say test and test1 Test.txt Code: Lun01 2TB 1.99TB 99.6% Lun02 2TB 1.99TB 99.5% Lun03 2TB 1.99TB 99.5% Lun04 2TB 1.55TB 89.6% Code: Test1.txt Lun01 2TB 1.99TB 89.5% Lun02 2TB 1.99TB 99.5% Lun03 2TB 1.99TB 99.5% Requirement is to compare... (6 Replies)
Discussion started by: venkitesh
6 Replies

3. UNIX for Dummies Questions & Answers

Display file with escaped color codes

Hi, I have a file containing color codes: Fri May 25 17:13:04 2012: Starting MTA: exim4^ Loading cpufreq kernel modules...^How can I display it colorized on a linux terminal? (4 Replies)
Discussion started by: ripat
4 Replies

4. Programming

Using ANSI color codes in gcc compiled program

I have put some yellow color codes and works well. I call the funstion using print_usage(stderr, 0); I would like to know if there is any way, to store the ansi color codes in variables and then call them inside fprintf. Or have a format followed by the strings I want to output. ... (5 Replies)
Discussion started by: kristinu
5 Replies

5. UNIX for Dummies Questions & Answers

Regular expression on hex color codes

I want to have all hex color codes in a given stylesheet in uppercase, so #fff should be converted to #FFF for instance. Here is the regular expression I use to match and convert hex color codes to uppercase: sed -e 's/^#({3}$)|({6}$)/^#({3}$)|({6}$)/' main.css However, no conversion to uppercase... (6 Replies)
Discussion started by: figaro
6 Replies

6. UNIX for Dummies Questions & Answers

Using color escape codes in less

Hi all, Not sure how "for dummies" this question is, but I'd better use understatement... A. My Environment ============== I am using RedHat Linux, version 2.6.18-53.el5. When I type less --version I get: less 394 Copyright (C) 1984-2005 Mark Nudelman ... My terminal is configured... (1 Reply)
Discussion started by: Source2Exe
1 Replies
Login or Register to Ask a Question