0

I am trying to extract some info from a text block with markers like #@ and #@@. Using the command below with the example file works, but when trying chain it with -e it does not work as expected.

Current command (not ideal)

sed -n "/^#@/,/#@@/p" file | sed 's/[#@]*//'

Reworked command ( does not work)

sed -en "/^#@/,/#@@/p" -e 's/[#@]*//' file

Desired output

text title
text line
text line

File format

#
#
#@ text title
#  text line
#  text line
#@@
#

What i am doing wrong ?

1

The command

sed -en "/^#@/,/#@@/p" -e 's/[#@]*//' file

will likely error out, because -en tries to apply expression n to file /^#@/,/#@@/p. If you want to combine -e with other options, you must put the expression argument after the -e like -ne "/^#@/,/#@@/p" or separate them completely like -n -e "/^#@/,/#@@/p"

However it looks like you want to apply the substitute command to the addressed lines and then print them, which is really a single expression:

$ sed -n '/^#@/,/#@@/s/^#@*//p' file
 text title
  text line
  text line

To remove leading whitespace as well:

$ sed -n '/^#@/,/#@@/s/^#[@ ]*//p' file
text title
text line
text line
2
  • Thanks. It seems it cannot mix other options with -e switch
    – ppk
    Oct 5 '20 at 20:00
  • 2
    @ppk well you can't mix -e and -n like -en 'expression' because that tries to run n as an expression on file expression. You should however be able to do -ne 'expression' Oct 5 '20 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.