简体   繁体   中英

linux pipe with egrep not working as expected

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 ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM