LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 07-12-2006, 09:50 PM   #1
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Rep: Reputation: 45
sed - the most confusing thing I have ever had to do


I'm just now getting around to learning sed and I am trying to do what I thought would be a very basic sed command, but it has turned into one of the most confusing things I've ever had to do in Linux

I don't know why this is so hard for me, but all I need to do is find the first line that contains $find and replace the entire line with $replace. Oh, and I forgot to mention I'm trying to do this from a perl script. After many hours of banging my head on the keyboard, I got it to work (assuming there is only one occurence of $find). The problem is that there is not only one occurence. Here is a snippet of the perl code calling sed, I never thought I would have to write code that had more symbols then letters
Code:
$find="browser.startup.homepage";
$replace="user_pref'\('\\\"'browser.startup.homepage'\\\"', ''\\\"'$homepg'\\\"''\)''\;'";
$cmd="sed -i -e '/'$find'/a'$replace'' -e '/'$find'/d' /etc/wcprofile/prefs.js";
system("$cmd");
For anyone that noticed, yes I'm trying to change the homepage in the mozilla prefs. Please don't respond with "why don't you just change the settings in the browser".

OK, now I've read everything I could find about sed and it seems there is a lot of conflicting information out there. This little blurb in the man page seems the most promising:
Quote:
q - Immediately quit the sed script without processing any more input, except that if auto-print is not disabled the current pattern space will be printed.
Unfortunately, I can't for the life of me figure out how to use it correctly. I still don't really get how the sed "commands" work, and how to use multiple commands at once (IE. delete then quit) I've tried experimenting with a lot of diffrent syntax, but no luck.

Maybe there is an easier way to change the homepage in prefs.js from a perl script?
Code:
user_pref("browser.startup.homepage", "http://www.google.com");
user_pref("browser.startup.homepage_override.mstone", "rv:1.8.0.4");
The second line is the one giving me all the headache. This needs to remain unchanged.

Could any of you gurus out there help a tortured soul?


thanks!
...drkstr
 
Old 07-12-2006, 11:28 PM   #2
cs-cam
Senior Member
 
Registered: May 2004
Location: Australia
Distribution: Gentoo
Posts: 3,545

Rep: Reputation: 57
Regex is probably one of the greatest inventions ever. Something to consider though, why not use some inbuilt regex module in Perl? Calling sed will mean you'll need to be escaping escape characters blah blah messy, as I see you've discovered
 
Old 07-13-2006, 02:49 AM   #3
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
Sheesh, I can't belive I did that!

I originally thought I would be able to avoid the work of having to write a perl script by just passing a sed command to the system. When it didn't work, I got so caught up in figuring it out that I completely forgot that the solution was harder then the problem.

Thanks for the advice, I pulled out Ye Olde Perl Book, blew the dust off, and got it working with the following code:
Code:
open (INF, "/etc/wcprofile/prefs.js") || die "Unable to open /etc/wcprofile/prefs.js for reading!";
@myPrefs=<INF>;
close (INF) || die "Error closing  /etc/wcprofile/prefs.js";
open (OF, ">/etc/wcprofile/prefs.js") || die "Unable to open /etc/securewc/profile/prefs.js for writing!";

$found=0;
foreach $line (@myPrefs) {

  if ( $line=~/browser\.startup\.homepage/ && !$found  ) {
    $line="user_pref\(\"browser.startup.homepage\"\, \"$homepg\"\)\;\n";
    $found=1;
  }
  print OF $line;
}
close (OF ) || die "Error closing  /etc/wcprofile/prefs.js";
not the prettiest, but it works. I can sleep easy now.

thanks again!
...drkstr
 
Old 07-13-2006, 12:37 PM   #4
cs-cam
Senior Member
 
Registered: May 2004
Location: Australia
Distribution: Gentoo
Posts: 3,545

Rep: Reputation: 57
Glad I could help. I don't even know Perl but often you know the solution yourself, just need an outside perspective to see it
 
  


Reply



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
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
this is so confusing!! theguitarness Linux - Newbie 5 03-07-2006 10:53 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
so confusing c12ayon Programming 5 10-26-2003 11:52 PM
Very confusing rm Luc Linux - Newbie 3 11-04-2002 01:42 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration