Question

I have this small piece of code

 egrep -oh '([A-Z]+)_([A-Z]+)_([A-Z]+)' -R /path | uniq | sort

I use this script to dig for environment variables inside files stored in a common directory when I don't want to display any duplicate, but I just want the the name of any variable if any are being used.

needless to say that the regex works, the matched words are the ones that are composed of 3 subsets of letters in uppercase *_*_*, the problem is that uniq doesn't look like it's work and doing anything, the variables are just printed out as egrep finds them.

Not even uniq -u does the trick.

Is the pipe itself the problem ?

Was it helpful?

Solution

uniq requires its input to be sorted if you want it to work in this manner. From the man page: (emphasis mine)

DESCRIPTION: Filter adjacent matching lines

So you could put a sort before the uniq in the pipeline, but that is not necessary, you can simply use the -u flag to sort to only output unique lines from the sorted output:

egrep -oh '([A-Z]+)_([A-Z]+)_([A-Z]+)' -R /path | sort -u
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top