LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-31-2008, 12:03 PM   #1
ilo
LQ Newbie
 
Registered: Jan 2008
Posts: 6

Rep: Reputation: 0
sed insert # at the beginning of a line


Hello Everyone I need to insert # [comment] at the beginning of a line in /etc/udev/rules.d/50-udev.rules

Any help appreciated
 
Old 01-31-2008, 12:06 PM   #2
teddyt
Member
 
Registered: Dec 2007
Location: US
Distribution: Slackware 12.1
Posts: 119

Rep: Reputation: 15
As best I can understand your problem, the easiest way is probably to log into a terminal as root

nano /etc/udev/rules.d/50-udev.rules

Make whatever changes you want. Hit Ctl-X to get out of nano and save your changes.
 
Old 01-31-2008, 12:46 PM   #3
Poetics
Senior Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,178

Rep: Reputation: 49
To insert a character at the beginning of a line with sed one can use the following notation (using '#' for example): s/^/#/
 
Old 01-31-2008, 12:50 PM   #4
ilo
LQ Newbie
 
Registered: Jan 2008
Posts: 6

Original Poster
Rep: Reputation: 0
^!!!

Thanks poetic I did not remember that ^ means at the beginning of the line

I will try now
 
Old 01-31-2008, 02:05 PM   #5
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
But, usually sed is used for changing things on a mass scale, not a single line in a single file. What exactly are you trying to do, just edit the file ? Or you want to make a script to do what ?
 
Old 01-31-2008, 03:45 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,974
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Quote:
Originally Posted by H_TeXMeX_H View Post
But, usually sed is used for changing things on a mass scale, not a single line in a single file.
Depends on your preferences, the task at hand and your knowledge
of the file to edit in question.

I reckon that if I *know* that line 19375 needs to be
commented my sed
Code:
sed -i '19375 s/^/#/' file
is going to be done quicker than someones pico (nano, whatever)




Cheers,
Tink
 
1 members found this post helpful.
Old 02-01-2008, 07:58 AM   #7
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
Well, ok, I guess it will be useful if you know the line number, but this a rare thing to do, and not too safe. What if some other program inserts a line somewhere. I sense danger
 
Old 02-05-2008, 08:37 AM   #8
ilo
LQ Newbie
 
Registered: Jan 2008
Posts: 6

Original Poster
Rep: Reputation: 0
sed worked

Hi All,
Thank you for all your reply.
I needed to change specific lines in the post script of a kickstart install and I just thought that sed would be much faster.

Thanks for all replies.
 
Old 02-05-2008, 12:30 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,974
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Quote:
Originally Posted by H_TeXMeX_H View Post
Well, ok, I guess it will be useful if you know the line number, but this a rare thing to do, and not too safe. What if some other program inserts a line somewhere. I sense danger
But then you have the same problem with a text editor
and a large file you don't know 100%, too. What if
you just skim through it for a pattern and don't realise
that it's in there several times? As for sed, you could
as easily (again, only if you know what you're after,
and whether it's unique or not) use a /pattern/ to
indicate the line you want changed rather than the #.


Cheers,
Tink
 
Old 02-05-2008, 12:32 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,974
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Quote:
Originally Posted by ilo View Post
Hi All,
Thank you for all your reply.
I needed to change specific lines in the post script of a kickstart install and I just thought that sed would be much faster.

Thanks for all replies.
Cool ;}


Cheers,
Tink
 
Old 02-05-2008, 01:14 PM   #11
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
It would probably be better to use the form: sed -i '/pattern/s/^/# /' file
where the pattern matches the line you want to edit.

For very long files, add a quit command as well so that the entire file isn't processed.
sed -i '/pattern/s/^/# /;/pattern/q' file

Last edited by jschiwal; 02-05-2008 at 01:17 PM.
 
Old 02-05-2008, 05:59 PM   #12
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
Hi.
Quote:
Originally Posted by jschiwal View Post
It would probably be better to use the form: sed -i '/pattern/s/^/# /' file
where the pattern matches the line you want to edit.

For very long files, add a quit command as well so that the entire file isn't processed.
sed -i '/pattern/s/^/# /;/pattern/q' file
Hmm. With the sed I use, this essentially truncates the file, does it continue to do the entire file on yours? If it does truncate, I would guess that this is not what is desired ... cheers, makyo
 
Old 02-05-2008, 06:49 PM   #13
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
It will truncate the file if you don't use the "-i" option, as in "sed '/pattern/s/pat1/pat2/q;/pattern/q' >newfile"

Anyway, it would work without the quit command as well. Selecting the line to edit based on a unique pattern was my main point.
 
Old 02-05-2008, 06:53 PM   #14
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,229

Rep: Reputation: 1019Reputation: 1019Reputation: 1019Reputation: 1019Reputation: 1019Reputation: 1019Reputation: 1019Reputation: 1019
I'm with makyo - truncates regardless on Ubuntu and Arch (sed 4.1.5 on both).
 
Old 02-05-2008, 11:12 PM   #15
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
I did triple check my results with a sample example, but I'll take your word for it. You could use the quit command if you are extracting information from a file.
Code:
jschiwal@hpamd64:~> cat test
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8

jschiwal@hpamd64:~> sed -i '/4/s/4/four/;/4/q' test
jschiwal@hpamd64:~> cat test
line 1
line 2
line 3
line four
line 5
line 6
line 7
line 8
Code:
sed --version
GNU sed version 4.1.5
 
  


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
Insert CRLF's with sed? unSpawn Programming 6 11-22-2006 07:46 AM
SED - replace / insert furquan Programming 5 03-01-2006 06:58 PM
insert string with sed greg108 Programming 7 02-18-2005 01:11 PM
insert a symbol with sed tonton Programming 5 08-31-2004 11:33 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM


All times are GMT -5. The time now is 03:27 PM.

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