LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-16-2010, 05:55 PM   #1
pkramerruiz
LQ Newbie
 
Registered: Jan 2010
Distribution: Ubuntu
Posts: 19

Rep: Reputation: 0
Question 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.
 
Old 10-16-2010, 09:47 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi,

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.
 
1 members found this post helpful.
Old 10-16-2010, 10:02 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
use awk
Code:
#! /bin/bash

awk '/CleanCache.*false/{ f=1 }
{ a[++d]=$0 }
END{
  if(!f){
    for(i=1;i<=d;i++){
      if (i==2 || i==(d)){
         a[i]="Cleancache \042true\042\n" a[i]
      }
      print a[i]
    }
  }
}' file
 
1 members found this post helpful.
Old 10-17-2010, 01:54 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873
Here is an awk alternative:
Code:
awk '/CleanCache.*false/{save = "Cleancache \042true\042";a=1;next}g{print save}{save=$0;g=1}END{if(a)print save;else print "Cleancache \042true\042\n" save}' file
 
1 members found this post helpful.
Old 10-17-2010, 03:15 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
Quote:
Originally Posted by grail View Post
Here is an awk alternative:
Code:
awk '/CleanCache.*false/{save = "Cleancache \042true\042";a=1;next}g{print save}{save=$0;g=1}END{if(a)print save;else print "Cleancache \042true\042\n" save}' file
i think OP wants "after the first line" as well
 
Old 10-17-2010, 05:41 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873
My bad ... I like yours then
 
Old 10-17-2010, 08:54 AM   #7
pkramerruiz
LQ Newbie
 
Registered: Jan 2010
Distribution: Ubuntu
Posts: 19

Original Poster
Rep: Reputation: 0
Thumbs up Thanks

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.
 
Old 10-17-2010, 09:32 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,411

Rep: Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873Reputation: 1873
I am not sure I see the point of this then as it would be a one time edit, why not just open with your favourite editor and do the job?
 
Old 10-17-2010, 09:49 AM   #9
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by pkramerruiz View Post
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.
 
1 members found this post helpful.
  


Reply

Tags
sed


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
[SOLVED] trying to insert text in the last line of a file with sed command.... Sayan Acharjee Linux - General 7 10-04-2010 05:00 AM
using sed to replace text on one line in a text file vo1pwf Linux - Newbie 5 06-24-2009 07:54 AM
Sed to read the file and replace and insert the pattern saurabhchokshi Programming 2 06-12-2009 01:15 PM
insert a single quote is text file with sed eln01 Programming 7 05-10-2007 11:02 AM
one liner needed for search and replace chr15t0 Linux - General 2 09-24-2002 10:00 AM


All times are GMT -5. The time now is 06:16 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