Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Seattle, WA, USA

    How to Uncomment a Single Line

    I received this PM from another forum user. I am a strong believer in answering questions on the forum itself, so I will answer it here.

    I've been trying to do something with sed and I can't figure it out. By reading the posts, your understanding of sed is light years ahead of me and I hope you can help me.

    I'm trying to use sed inline sed -i to find a specific line in a file and remove the first character which is a #. Yes just find a line and uncomment it.

    Say I have:
    I just want to write
    in the same spot in the file

    I have found other ways of doing it like grep'ing for the line and running it thru sed to strip the comment but I'm sure there is a more elegant way of doing it.
    The basic format of what you said is correct, but grep is unnecessary here. sed is capable of finding lines on its own.

    If we assume that the line has a particular pattern, it is very simple:
    sed -re '/PATTERN/ s/^#//' file
    This takes any line that matches PATTERN, and replaces the '#' at the beginning of the line with nothing, essentially deleting the line.

    On the other hand, if there is no pattern, but it is a particular line number, it is even easier:
    sed -re '5 s/^#//' file
    This takes line 5 and removes the '#' at the beginning.

    I hope that this answers the question.

  2. #2
    Thank you.
    Very simple once you know how to do it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts