LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-19-2008, 06:59 PM   #1
Romanus81
Member
 
Registered: Feb 2008
Posts: 201

Rep: Reputation: 31
Comment/uncomment lines?


I am trying to write a bash script that will remove a comment out of the beginning of a line, so far I know that with the command, [vi +4 file] it will open to line four of the file I need to edit, all I need is for the script to add or remove the comment. I don't know if there is a simpler way or some function of vi I am not understanding.
 
Old 03-19-2008, 07:28 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
So you want to remove all instances of ‘#’ at the beginning of the fourth line?

How about:
Code:
sed -i '4s/^#*//' file
Or if you must use vi/ex:
Code:
ex -c '4s/^#*//|wq' shell.sh
 
Old 03-27-2008, 09:07 PM   #3
Romanus81
Member
 
Registered: Feb 2008
Posts: 201

Original Poster
Rep: Reputation: 31
Ok, that worked, thank you. I used the method with sed.
But how do I add it back in?
 
Old 03-27-2008, 09:13 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by Romanus81 View Post
But how do I add it back in?
You mean you want to add a hash mark to the beginning of line 4?
Code:
sed -i '4s/^/#/' file
 
Old 03-31-2008, 08:25 PM   #5
Romanus81
Member
 
Registered: Feb 2008
Posts: 201

Original Poster
Rep: Reputation: 31
thank you so much, this makes my life a lot easier!
 
  


Reply



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
http.conf - comment or uncomment AddType since html files not showing in browser? praisehim Linux - Newbie 1 11-28-2007 10:38 AM
listing non-comment lines scbops Linux - Newbie 5 09-18-2007 08:55 PM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
Vi view uncomment lines only dhammika Linux - General 8 10-01-2005 02:05 AM
vsftpd - comment/uncomment question. zaoka Linux - General 1 07-31-2005 01:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:13 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration