Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 ?
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
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.
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 #.
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.
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.