Skip to main content
3 of 5
deleted 3 characters in body
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60

Based on what you've told us so far this might be what you're trying to do, using any awk:

$ awk -F'[][]' '{print $2}' file
Some title

but without more truly representative sample input and expected output that's just a guess.

As for what's wrong with your sed script:

sed 's/^\(\#*\) *\[\([^\]]*\)\].*/\1 \2/'

In the first regexp segment \(\#*\):

  1. You're escaping the literal char # as \# which is undefined behavior per POSIX when you wanted just #.
  2. You're using #* which matches zero or more #s when you wanted 1 or more which is ##* (or #+ if you were using an ERE).

In the separating spaces part *:

  1. You're using <blank>* which matches zero or more #s when you wanted 1 or more which is <blank><blank>* (or <blank>+ if you were using an ERE).

In the last regexp segment \[\([^\]]*\)\].*:

  1. You're using [^\]] and so escaping ] which is undefined behavior per POSIX when you wanted just [^]].
  2. You're using \] at the end which is undefined behavior per POSIX since there's no unescaped [ before it when you wanted just ].

If you fixed all of those issues you'd get:

$ sed 's/^\(##*\)  *\[\([^]]*\)].*/\1 \2/' file
## Some title

and since you're using GNU sed which supports EREs you could write that as:

$ sed -E 's/^(#+) +\[([^]]*)].*/\1 \2/' file
## Some title

And then to leave just the title as you said you wanted just means removing the first capture group:

$ sed 's/^##*  *\[\([^]]*\)].*/\1/' file
Some title

$ sed -E 's/^#+ +\[([^]]*)].*/\1/' file
Some title
Ed Morton
  • 35.8k
  • 6
  • 25
  • 60