LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   sed insert # at the beginning of a line (https://www.linuxquestions.org/questions/linux-newbie-8/sed-insert-at-the-beginning-of-a-line-617707/)

ilo 01-31-2008 12:03 PM

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

teddyt 01-31-2008 12:06 PM

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.

Poetics 01-31-2008 12:46 PM

To insert a character at the beginning of a line with sed one can use the following notation (using '#' for example): s/^/#/

ilo 01-31-2008 12:50 PM

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

I will try now

H_TeXMeX_H 01-31-2008 02:05 PM

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 ?

Tinkster 01-31-2008 03:45 PM

Quote:

Originally Posted by H_TeXMeX_H (Post 3041433)
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

H_TeXMeX_H 02-01-2008 07:58 AM

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 :)

ilo 02-05-2008 08:37 AM

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.

Tinkster 02-05-2008 12:30 PM

Quote:

Originally Posted by H_TeXMeX_H (Post 3042382)
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

Tinkster 02-05-2008 12:32 PM

Quote:

Originally Posted by ilo (Post 3046797)
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

jschiwal 02-05-2008 01:14 PM

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

makyo 02-05-2008 05:59 PM

Hi.
Quote:

Originally Posted by jschiwal (Post 3047013)
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

jschiwal 02-05-2008 06:49 PM

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.

syg00 02-05-2008 06:53 PM

I'm with makyo - truncates regardless on Ubuntu and Arch (sed 4.1.5 on both).

jschiwal 02-05-2008 11:12 PM

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



All times are GMT -5. The time now is 07:33 AM.