-   Linux - Newbie (
-   -   Adding # to the first line (

leblinux 06-23-2011 08:37 AM

Adding # to the first line

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.


colucix 06-23-2011 08:43 AM


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

druuna 06-23-2011 08:43 AM


To add:

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

To remove:

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

Hope this helps.

Snark1994 06-23-2011 08:43 AM

To add:


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


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

leblinux 06-23-2011 09: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 09:11 AM


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 09:12 AM

Here we go:

All times are GMT -5. The time now is 02:50 PM.