Extract text between two square [..] brackets

Hi All,

After searching about this, I could find some solutions but I am not sure why it is not working in my case.

I have a text file with contents between two square brackets. The text file looks like this:

Use [code_one] tags when you post any code so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code_two] and [/code_three] by hand.)

I want to extract all the text that is between the square brackets [...] that is my output should be like this:

code_one
code_two
/code_three

This is what I have tried but it gives an error:

more file_name | sed 's/.*[ //' | sed 's/].*$//'

and the error that I get is this:

I am using Linux with Bash.

$
$
$ # show the contents of the data file "f0"
$ cat f0
Use [code_one] tags when you post any code so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code_two] and [/code_three] by hand.)
$
$
$ # use a Perl one-liner
$ perl -lne 'print $1 while (/\[(.*?)\]/g)' f0
code_one
code_two
/code_three
$
$
$

tyler_durden

Try:

awk 'NR>1{print $1}' RS=[ FS=]
1 Like