2

How to print the content between two brackets.

For example:

return [ "server1.domain.com" ,

"server2.domain.com" ,

"server3.domain.com" ]

Expected result:

   "server1.domain.com" ,

   "server2.domain.com" ,

   "server3.domain.com"

How to get the expected result?

рüффп
  • 1,677
  • 4
  • 27
  • 35
manth
  • 23
  • 3

2 Answers2

4

With pcregrep:

pcregrep -Mo 'return \[\K[^]]*'

Multiline match on return [ followed by a sequence of non-] characters but only output the part to Keep (to the right of \K).

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
  • Thank you for the reply.. Is it possible to do this with grep command. – manth Sep 14 '16 at 12:52
  • 2
    @manth, `pcregrep` is one of many `grep` implementations. Some other `grep` implementations like some versions of GNU `grep` will also work there if you replace the `pcregrep -Mo` with `grep -zPo` and pipe the output to `tr '\0' '\n'` – Stéphane Chazelas Sep 14 '16 at 12:56
1

With sed

sed '
    /.*\[ /{
        s///        #remove all upto open square bracket 
        :a          #return mark
        /\ ].*/!{
            N       #get next line untill there is close square bracket
            ba      #back to return mark
        }
        s///        #remove all from close square bracket to end of line 
     }
     '
Costas
  • 14,806
  • 20
  • 36