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 |
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. |
To insert a character at the beginning of a line with sed one can use the following notation (using '#' for example): s/^/#/
|
^!!!
Thanks poetic I did not remember that ^ means at the beginning of the line
I will try now |
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 ?
|
Quote:
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 Cheers, Tink |
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 :)
|
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. |
Quote:
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 |
Quote:
Cheers, Tink |
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 |
Hi.
Quote:
|
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. |
I'm with makyo - truncates regardless on Ubuntu and Arch (sed 4.1.5 on both).
|
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 Code:
sed --version |
All times are GMT -5. The time now is 07:33 AM. |