LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 06-23-2011, 07:37 AM   #1
leblinux
Member
 
Registered: Dec 2008
Posts: 46

Rep: Reputation: 0
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.
 
Old 06-23-2011, 07:43 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Code:
$ sed -i '1s/^/#/' file   # add
$ sed -i '1s/^#//' file   # remove
 
1 members found this post helpful.
Old 06-23-2011, 07:43 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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.
 
1 members found this post helpful.
Old 06-23-2011, 07:43 AM   #4
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,630
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
To add:

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

Code:
sed -e '1s/^#//' filename
EDIT: beaten to it... twice.
 
1 members found this post helpful.
Old 06-23-2011, 08:00 AM   #5
leblinux
Member
 
Registered: Dec 2008
Posts: 46

Original Poster
Rep: Reputation: 0
Thanks guys! I need to google for a good tutorial... since sed has alots of mumbojumbos :0) /\/\/'s/g'{/\/\/\ etc.. )
 
Old 06-23-2011, 08:11 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
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.
 
1 members found this post helpful.
Old 06-23-2011, 08:12 AM   #7
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Here we go: http://www.grymoire.com/Unix/Sed.html
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] adding new line after each line in perl KManepalli Linux - Newbie 9 04-28-2011 08:02 AM
[SOLVED] adding line from file1 into a line of another file based on maching IDs rossk Programming 6 01-06-2011 12:06 AM
Preprocessor adding new line BJesty Programming 3 04-07-2009 11:26 AM
Adding a line to file mikz Linux - General 2 03-03-2005 12:35 PM
adding line to /etc/ld.so.conf jotus Red Hat 3 10-13-2003 05:49 AM


All times are GMT -5. The time now is 05:55 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration