LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH: sed question (https://www.linuxquestions.org/questions/programming-9/bash-sed-question-798118/)

worm5252 03-26-2010 02:45 PM

BASH: sed question
 
hey guys,
I don't get regex and I need to figure out how to use sed to uncomment some stuff in /etc/httpd/conf/httpd.conf.

I need to find the following text
Code:

# <Location /server-status>
#    SetHandler server-status
#    Order deny,allow
#    Deny from all
#    Allow from .example.com
# </Location>

and I need to uncomment it so it looks like this
Code:

<Location /server-status>
    SetHandler server-status
    Order deny,allow
    Deny from all
    Allow from .example.com
</Location>

Since I don't understand regex yet and I have to have this done soon, can someone pop out some code for me to use? Thanks

druuna 03-26-2010 03:05 PM

Hi,

The following will look for a range (starting with: # <Location /server-status> and ending with: # </Location>. If found the first # and space are removed:

sed '/# <Location \/server-status>/,/# <\/Location>/s/^# //' infile

Do check if the output is correct before making it definite, the range could be "greedy" (larger range then expected) due to possible inconsistencies in the input file.

Hope this helps.

worm5252 03-26-2010 03:12 PM

Thank You. I will give this a go

worm5252 03-26-2010 03:18 PM

Didn't work, it made a copy of all of that code and a few lines above it and pasted them below it. Instead of deleting "# " it just deleted the space. so I am left with
Code:

#<Location...
instead of
Code:

<Location...

druuna 03-26-2010 03:31 PM

Hi,

Quote:

it made a copy of all of that code and a few lines above it and pasted them below it.
That, I don't understand......

Could you paste a relevant part of the input file (including what's above and below).

This happens with your example from post #1 and the sed command I supplied:
Code:

$ cat infile
  something here
</Location>

# <Location /server-status>
#    SetHandler server-status
#    Order deny,allow
#    Deny from all
#    Allow from .example.com
# </Location>

<Location /server-uptime>
  more here
$ sed '/# <Location \/server-status>/,/# <\/Location>/s/^# //' infile
  something here
</Location>

<Location /server-status>
    SetHandler server-status
    Order deny,allow
    Deny from all
    Allow from .example.com
</Location>

<Location /server-uptime>
  more here
$


worm5252 03-26-2010 03:33 PM

No worries, I got it sorted. I tinkered with your expression and figured it out. I just removed all the spaces and it works fine.


All times are GMT -5. The time now is 07:26 PM.