3

I have a file with many curly bracketed words, ex. {test1}, {test2} - how would I grep for these words and output a distinct list of them?

output example:

{test1}
{test2}

I've tried this which didn't work:

grep -o {.*} file.xml  | sort | uniq

UPDATE

Actually - this seems to have worked:

grep -oP '{.*?}' file.xml | sort | uniq
slm
  • 369,824
  • Welcome to Unix & Linux! If you've determined your own solution you can write it up as the answer. – slm Jul 22 '13 at 14:59

1 Answers1

5

There are two problems with your first attempt. {.*} contains special characters which need to be protected from expansion by the shell; put quotes around the pattern. Also, {.*} matches the longest brace-delimited text on the line, so if you have a line containing hello {test1} world {test2} howdy then the output is {test1} world {test2} becaause .* matched test1} world {test2.

The following will output only what is between brackets.

grep -o -e "{[^}]*}" 

My original formulation used "{.*}", but with that the widest bracket found within a line, not the smallest one would be returned...

soulsource
  • 375
  • 3
  • 9