Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Awk or grep question

Tags:

grep

awk

I have this datafile

[abc]
def
ghi
[jkl]
[mno]

From this file; i can run grep and easily get all lines that have "[" in them. How can I get the contents of text inside "[]".

For example:

abc
jkl
mno

Thanks

like image 692
shergill Avatar asked Sep 18 '26 12:09

shergill


1 Answers

Give this a try:

sed -n 's/\[\([^]]*\)\]/\1/p'

or

awk -F "[][]" '$2 != "" {print $2}'

or

grep -Po '(?<=\[)[^]]*(?=])'
like image 113
Dennis Williamson Avatar answered Sep 22 '26 06:09

Dennis Williamson