LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Adding # to the first line (http://www.linuxquestions.org/questions/linux-newbie-8/adding-to-the-first-line-887925/)

leblinux 06-23-2011 07:37 AM

Adding # to the first line
 
Dears,

How Does sed or other command edit the 1st line in a file and comments it? Meaning add "#" to the first line in a file for example?

Summary: Insert # to the first line.

Before: Comment me please.
sed command or another command

After: #Comment me please.

------And if possible a way to remove the "#" using command line.

Thanks.

colucix 06-23-2011 07:43 AM

Code:

$ sed -i '1s/^/#/' file  # add
$ sed -i '1s/^#//' file  # remove


druuna 06-23-2011 07:43 AM

Hi,

To add:
Code:

sed '1s/^/#/' file
The 1 limits it to the first line only.

To remove:
Code:

sed '1s/^#//' file
The ^ checks if it is the first character on the line.

Hope this helps.

Snark1994 06-23-2011 07:43 AM

To add:

Code:

sed -e '1s/\(.*\)/#\1/' filename
To remove:

Code:

sed -e '1s/^#//' filename
EDIT: beaten to it... ;) twice.

leblinux 06-23-2011 08:00 AM

Thanks guys! I need to google for a good tutorial... since sed has alots of mumbojumbos :0) /\/\/'s/g'{/\/\/\ etc.. :))

druuna 06-23-2011 08:11 AM

Hi,

Here's one site that deals with sed: Sed - An Introduction and Tutorial by Bruce Barnett

If you like paper books, have a look at O'Reilly's Sed & Awk (isbn 1-56592-225-5)

Hope this helps.

colucix 06-23-2011 08:12 AM

Here we go: http://www.grymoire.com/Unix/Sed.html


All times are GMT -5. The time now is 09:59 AM.