Selecting data between [ and ]

Hi Team,
I am searching through log file using

grep -i '<search_key>' <file_name>|awk '{print $18}'

example outputs are

[MY_DWH]
[MY_CATEGORY]
[MY_LEARNING_DATA]

I would like to select the data between [ and ] and no other as I am getting some junk characters sometimes which chaging the o/p display format.
Kindly assist. Thanks in advance.

Thanks
Sam

If the junk characters aren't [], I'm not sure how removing [] will help, but:

awk '{ substr($8,2, length($8)-2); }'

If that doesn't do it, you'll need to actually explain what your input data is, so we know why $8 doesn't always work.

grep -i '<search_key>' <file_name>|awk '{print $18}'| sed 's/\[\([^]]\+\)\]/\1/g'

@skrynesaver

somewhere I see a extra character on wc -c ..
thanks for your help anyways .
I must admit that I am newbie in sed

---------- Post updated at 12:40 PM ---------- Previous update was at 12:11 PM ----------

i found out something like this

grep -i '<search_key>' <file_name>|awk '{print $18}'|cut -d'[' -f2|cut -d']' -f1

any other way ?

Did you try my way yet?

grep -i '<search_key>' <file_name> | awk '{gsub(/[\]\[]/,"",$18); print $18}'

tyler_durden

@corona688 :

Its not working ...

Try this. Untested.

grep -i '<search_key>' <file_name> | perl -pe 's/.*?(\[.*?\]).*/$1/g; tr/[]//d'

Did you change $8 to $18 to match your field?