[SOLVED] sed one-liner: search text in file and replace it. if not present, insert it
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
sed one-liner: search text in file and replace it. if not present, insert it
As indicated in the subject, I want to search a text.
If the text is present I want to replace it. But if the text is not present, I want to insert it after first line and before last line.
Searched text is:CleanCache "*";
Where * can be anything.
Example: CleanCache "false"; -> CleanCache "true";
If CleanCache "false"; is not present, only insert CleanCache "true"; after first line and before last line.
although it is possible to do all this with one sed instruction I do not think that this would be the best solution in this case. You would make a mess in the Hold buffer and then by the end would try to modify it with a more or less complicated regex.
Try this instead
Code:
sed -n '/pattern/ q 99' infile
if [[ $? = 99 ]]; then
sed -r 's/pattern/newpattern/' infile > outfile
else
sed -r '1 a \
newpattern
$ i \
newpattern
' infile > outfile
fi
Easy to understand, without unnecessary complications.
FWIW, here is one possible solution with just one sed. However, this does not *really* qualify as a one-liner.
Code:
# not recommended
sed -nr '1 h; 1 ! H; $ {g;s/pattern/newpattern/;t end; s/^([^\n]*)(.*\n)([^\n]*)$/\1\nnewpattern\2newpattern\n\3/;:end p}' infile > outfile
Both examples assume that there are no empty lines at the beginning and at the end of the file.
Many many thanks to all for your effort.
@crts, @ghostdog74:
I really need some art of one liner… Because I don’t want to create first a script and after the script has finished, delete the script… Isn it a little redundant and unnecessary?
To help a little: The file I want to edit is "/root/.synaptic/synaptic.conf"
and what I must insert is the following:
CleanCache "true";
AutoCleanCache "true";
delHistory "1";
Its a bit of non-graphical tuning of the synaptic-app!
Cheers & thanks! *Thumbs up**Thumbs up*
Last edited by pkramerruiz; 10-17-2010 at 09:03 AM.
Many many thanks to all for your effort.
@crts, @ghostdog74:
I really need some art of one liner… Because I don’t want to create first a script and after the script has finished, delete the script… Isn it a little redundant and unnecessary?
To help a little: The file I want to edit is "/root/.synaptic/synaptic.conf"
and what I must insert is the following:
CleanCache "true";
AutoCleanCache "true";
delHistory "1";
Its a bit of non-graphical tuning of the synaptic-app!
Cheers & thanks! *Thumbs up**Thumbs up*
Hi,
you can put the last 'sed' script in one line in the console and will make the changes you need. You just need to modify 'pattern/newpattern' accordingly to what you want. I said that it does not qualify as a one-liner just because I personally consider a sed statement as one liner if it does not exceed five commands.
If you add the -i.bak option you won't even have to redirect the output to a new file and it will also make a backup of the processed file.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.