1

Not very familiar with complex usages of sed, but I have this requirement now. I have a file 1.txt with contents in the following pattern:

sree@amby01:/home/sree : cat 1.txt
SSL
 this first line
#comment line
 second line
# third comment
# line nline
#ApiText
lkjfl
# my line
your line
sree@amby01:/home/sree :

Now, I want to remove the commented lines between the block SSL....#ApiText, but the line #ApiText should not be removed.

The output should look like this:

SSL
this first line
 second line
#ApiText

Its a bash shell. I know how to remove commented lines alone, but not how to remove commented lines except when the comment is following by a certain text. I can use only sed.

1
  • 2
    "I can only use sed"... Um... why?
    – Kusalananda
    Jun 29, 2016 at 10:06

1 Answer 1

6

There are many ways but one from several can be:

sed '/SSL/,/#ApiText/{//b;/^#/d}' 1.txt
  • /SSL/,/#ApiText/ - address range where commands will be executed
  • // - for line(s) with previous regexp (SSL or #ApiText - any of found)
  • b - go to end of instructions ( mean to do nothing)
  • /^#/d - delete(do not print) lines which starts by #
3
  • could you also explain briefly how this works?
    – Sundeep
    Jun 29, 2016 at 15:28
  • @spasic Code is very simple but if you like
    – Costas
    Jun 29, 2016 at 17:18
  • 1
    @Costas thanks, when you put it like that, it feels very simple.. but before your explanation it was all greek to me
    – Sundeep
    Jun 30, 2016 at 2:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .