tags:

views:

47

answers:

2

I've the CSS file with many entry like

#id1, #id2, #id3, #id4 { ... }
#id3, #id2 { ... }
#id2, #id4 { ... }

I want to extract list of unique IDs using command line tools (msys). Unique means any entry in list presented only once. How?

PS: I know how doing it using python, but what about awk/sed/cat?

+1  A: 

Try this

cat file.css | grep -o '#[a-zA-Z0-9-]*' | sort -u

Edit: unfortunately, this regexp is very crude and will allow hex colors to pass as IDs..

If css in your files are such that opening { is ALWAYS on the same line with #ids, then you an use this syntax:

cat file.css | grep { | cut -d'{' -f1| grep -o '#[a-zA-Z0-9-]*' | sort -u
mr.b
`[a-zA-Z0-9-]`.
SLaks
@SLaks: thanks, I totally forgot about -. And there were two matching sets in attempt to not collect hex colors, but it didn't work anyway.
mr.b
http://uuoc.com/ ;-)
Johnsyweb
@Johnsyweb: old habits die hard :) Good point though..
mr.b
A: 

In awk it may be easier:

awk -F'[,[:space:]]' -v RS=# 'NF{if (!a[$1]++) print $1}' file.css

though this code doesn't check for the presence of '{ ... }'.

marco