LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 07-11-2008, 02:28 PM   #1
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Rep: Reputation: 49
Adding the character "#" in a file using sed


Here is how my current file (module.txt) looks like:
Code:
#Un-comment the following line to load the IDE-Floppy disk drive driver "floppy"
#floppy

# Generated by sensors-detect on Thu Jun  5 17:05:10 2008
# I2C adapter drivers
# modprobe unknown adapter CS5536 ACB0
# Chip drivers
lm90

# Generated by sensors-detect on Fri Jan 25 15:40:21 2008
# Chip drivers
dme1737
and I want it to look like this after testing a particular condition in my shell script (if [ SOMETING = TRUE ] then)
[CODE]
Here is how my current file (module.txt) looks like:
Code:
#Un-comment the following line to load the IDE-Floppy disk drive driver "floppy"
#floppy

# Generated by sensors-detect on Thu Jun  5 17:05:10 2008
# I2C adapter drivers
# modprobe unknown adapter CS5536 ACB0
# Chip drivers
lm90

# Generated by sensors-detect on Fri Jan 25 15:40:21 2008
# Chip drivers
#dme1737
Basically I need to append "#" character at the beginning of the line. (see the blue color above)

Thanks
 
Old 07-11-2008, 02:42 PM   #2
ramram29
Member
 
Registered: Jul 2003
Location: Miami, Florida, USA
Distribution: Debian
Posts: 848
Blog Entries: 1

Rep: Reputation: 47
You could do something like:

cp file /tmp/file2

if [ SOMETING = TRUE ]
then
sed s/^dme1737/\#dme1737/g /tmp/file2 > file
fi



Test it first by running `sed s/^dme1737/\#dme1737/g file`
 
Old 07-11-2008, 02:49 PM   #3
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,249

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by ramram29 View Post
You could do something like:

cp file /tmp/file2

if [ SOMETING = TRUE ]
then
sed s/^dme1737/\#dme1737/g /tmp/file2 > file
fi



Test it first by running `sed s/^dme1737/\#dme1737/g file`
Thanks for your reply. I do understand what you are trying to do with the above commands however I have couple of questions with regards to that:

1. Why do we use "^" character before dme1737. Simply telling "dme1737" (without "^") won't do?

2. Also why the character "\" before "#dme1737"?

In short we do want to use the substitute function (/s) won't giving the following command:

Code:
 sed s/dme1737/#dme1737/g /tmp/file2 > file
work?

This is only for my understanding.
 
Old 07-11-2008, 02:55 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
The carat matches the beginning of a line.

Your input pattern has "dme1737" at the beginning of the line, so "^dme1737" matches this pattern.
Using just "dme1739" may work as well but if this pattern exists elsewhere you will have a
false positive match.

Also consider
sed '/^dme1737/s/^/#/' file >newfile.
Since we selected the line with the pattern "^dme1737", we can simply insert a # on that line.

There are often a number of solutions. You need to be accurate enough in your regular expressions
to avoid false matches or misses. So adding ^ before dme1737 would be a good idea. The pattern could be contained in a comment for example. If this where for an automated script that you will use for a while or publish with software, you may want to be even more precise ( matching only dme1737 and not dme1737b for example ) in order to future proof your script.

As far as I know, the backslash isn't needed to escape the # character.

Last edited by jschiwal; 07-11-2008 at 03:25 PM.
 
Old 07-11-2008, 02:59 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
You don't want the "\" before the "#"

"\" is typically used to "escape" a character--which is a funny way of saying change its meaning. "#" is not special in sed, so it does not need to be escaped.

"^" means "at the beginning of the line". You don't need it if there is no other instance of the pattern.
 
  


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
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 06:58 AM
how to add "pipes" to a sed program file new_2_unix Linux - Newbie 2 10-07-2007 09:09 PM
Replacing "function(x)" with "x" using sed/awk/smth Griffon26 Linux - General 3 11-22-2006 10:47 AM
Removing "#" character in a huge txt file tmaxx Linux - General 3 10-24-2006 03:04 AM
Adding a "Subscribe" button next to "Reply"? pnellesen LQ Suggestions & Feedback 2 06-21-2005 09:12 PM

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

All times are GMT -5. The time now is 08:12 AM.

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