1

I am aware of several other questions related to this one such as: Sed Unknown Option to s; however I am not having that problem. I am trying to run:

sed -n '/Ce./,/EOF/ {s!^#!! d} p' more_tests_high.job

but I keep getting:

sed: -e expression #1, char 21: unknown option to `s'

I am trying to search more_test_high.job for text between Ce and EOF, but remove any comment lines which start with #. Yes, EOF is a literal text in the file that I want to search for. I have tried using / , !, and _ as delimiters. I can run:

sed -n '/Ce./,/EOF/ p' more_tests_high.job 

and see all the text that is between Ce and EOF, but how do I remove the commented lines that start with #?

Community
  • 1
  • 1
Chemist612
  • 13
  • 6
  • 2
    You need a semicolon after the `!!` before the `d`. And you need to think what you're doing; you edit the line and then delete it without printing what you just changed. – Jonathan Leffler Mar 24 '16 at 17:25
  • @JonathanLeffler I added the semicolon after '!!' and then included the p inside the '}', but I don't get any output now. – Chemist612 Mar 24 '16 at 17:28
  • You want `{/^#/d}`, I think. You don't need `s` to delete lines. – Benjamin W. Mar 24 '16 at 17:29
  • That did the trick if you want to submit an answer I'll accept it as correct. For the record the final total is `sed -n '/Ce./,/EOF/ {/^#/d;p}' more_tests_high.job` – Chemist612 Mar 24 '16 at 17:35

3 Answers3

2

Your command should look like this:

sed -n '/CE./,/EOF/{/^#/d;p}' more_tests_high.job

For all the lines between the CE and the EOF line, you check if they are a comment line, and if yes, you delete it, which restarts the cycle and ignores the p.

If it's not a comment line, it will be printed.

BSD sed (also found on Mac OS X) requires an extra semicolon between the p and the closing brace.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
0

awk to the rescue!

 awk '/Ce./,/EOF/{if($0!~/^#/) print}' file

almost direct translation of the requirements without cryptic syntax.

karakfa
  • 66,216
  • 7
  • 41
  • 56
0

This might work for you (GNU sed):

sed '/CE./,/EOF/!d;/^#/d' file

You are only interested in the range of lines between CE. and EOF therefore anything else delete. Once in the required range, delete any lines that begin #. Print all remaining lines.

potong
  • 55,640
  • 6
  • 51
  • 83