I want to replace the default listening port of httpd to 9090. I can edit the line in httpd.conf file using below

sed -i "/^Listen/c\Listen 9090" /etc/httpd/conf/httpd.conf

But the line

Listen 80

may have white space before it.

How do I ignore this white space to match this line?


Change your matching pattern no catch white spaces before liste in the following way:


That will include all

Listen ..
    Listen ...

and others.

| improve this answer | |
  • Matching exactly as required. But it will eat up the white space in the replacement as well. But that should not be a problem. Thanks! – user1263746 Sep 9 '13 at 7:43
  • 4
    You can put the whitespace in a group and use that in the replacement. For example, s/^(\s*)Listen(.*)$/\1Listen/ (or something much like it; try it before you use it on anything valuable). – a CVn Sep 9 '13 at 7:56
  • Thanks Michael that's exactly what I was hoping for. Small fix: the parentheses must be escaped as so: s/^\(\s*\)Listen(.*)$/\1Listen/ – pmont Jan 14 '15 at 17:07
  • 1
    @pmont with gnu sed you can use sed -r instead escaping. – rush Jan 14 '15 at 18:11

Just allow the regular expression to match whitespace as well. You can use either the character group [ \t] (which matches the space or tab characters) or the equivalent POSIX bracket expression [:blank:].

Hence, /^Listen/ becomes /^[[:blank:]]*Listen/. (Note that the bracket expression is effectively written with a double square bracket pair.)

| improve this answer | |
  • Working perfectly. And same as the accepted answer, except that the accepted answer is more readable. Thanks! – user1263746 Sep 9 '13 at 7:47

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.