LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


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

Rep: Reputation: 45
can't seem to translate sed to perl with s2p


I am trying to convert a sed command to perl with s2p. The following sed command will work as entered:
Code:
sed -i -e '/browser[.]startup[.]homepage/auser_pref\("browser.startup.homepage", "'$homepg'"\)\;' \
       -e '/browser[.]startup[.]homepage/d' /etc/wc_profile/prefs.js
This will append the line setting my new homepage, then delete the old line.

Now I need to turn this command into perl and I heard of a little utility called 's2p'. In order to run s2p, I need to put the sed command into a sed script, so I put the following in 'sedscrpt':
Code:
/browser[.]startup[.]homepage/auser_pref\("browser.startup.homepage", "'$homepg'"\)\;
/browser[.]startup[.]homepage/d
Then I ran
Code:
s2p -f sedscrpt
but I get the following error:
Code:
s2p: sedscrpt l.1: invalid address after `,'
I've tried playing around with different backslash combinations, but can't seem to get it to go. Any ideas what I'm doing wrong?

thanks!
...drkstr

Last edited by drkstr; 07-12-2006 at 02:23 AM.
 
Old 07-12-2006, 01:49 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Try these specialists: http://www.perlmonks.com/
 
Old 07-12-2006, 01:56 AM   #3
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
I also noticed if I try and run the sed script directly, It no longer expands the variable.
Code:
export homepg=http://www.google.com
sed -f sedscrpt /etc/wc_profile/prefs.js
See above post for contents of sedscrpt.

I tried playing around with it and have not been able to get sed expand the variable in any way, when being run from a sed script.

"$homepg"
""$homepg""
\""$homepg"/"
\"'$homepg'\"

No matter what I try, it parses the text as is. Is it not possible to use variables in a sed script?

any ideas?

thanks!
...drkstr
 
Old 07-12-2006, 02:17 AM   #4
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
Quote:
Try these specialists: http://www.perlmonks.com/
Thanks for the link. You must of responded while I was typing my last message. I didn't see anything there that related, but I will poke around a bit more when I'm not so tired.

regards,
...drkstr

**edit**
If I can't figure this out, I will probably just call the sed command directly from the CGI script. I was hopeing I could avoid using a system call though (for security reasons).
**edit**

Last edited by drkstr; 07-12-2006 at 02:21 AM.
 
Old 07-13-2006, 09:12 AM   #5
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Even w/o having your /etc/wc_profile/prefs.js, I successfully duplicated your problem. Unfortunately I could not duplicate your results w/o an appropriate target file.

Some points:
  • The s2p error msg. should apply to the orig. script: what is that comma doing there?
  • According to the s2p man page on my system, you do not have to put your script in a a file, the same "-e" option will work just fine.
  • Those "monks" can probably help you solve the problem de novo, but they are probably mostly too exalted to touch sed. -- In Learning Perl they say not to bother learning sed or awk.
Personally, I can't manually parse your orig. script; so do you want to start over in Perl, or figure out what's wrong. If the latter, then you could start by explaining what you think each piece of the sed script is doing. Suggest you supply a few (<10) lines of the target file for testing.

Last edited by archtoad6; 07-13-2006 at 09:49 AM. Reason: speling
 
Old 07-13-2006, 11:42 AM   #6
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
Quote:
Personally, I can't manually parse your orig. script; so do you want to start over in Perl, or figure out what's wrong.
Thanks for the help! I actually allready got it working with perl, but I would still like to know how to do this (I can't sleep at night if I don't know how to do something )

Quote:
Suggest you supply a few (<10) lines of the target file for testing
Here is a snippet from the prefs.js that I am working with. The line in bold is the one I need to modify. The end result should be a change in the url listed for the homepage (defined by the user and assigned to $homepg)
Code:
user_pref("browser.link.open_newwindow.restriction", 0);
user_pref("browser.preferences.privacy.selectedTabIndex", 5);
user_pref("browser.search.selectedEngine", "Google");
user_pref("browser.startup.homepage", "http://www.drkstr.org");
user_pref("browser.tabs.warnOnClose", false);
user_pref("dom.disable_window_flip", true);
user_pref("dom.disable_window_move_resize", true);
Quote:
you could start by explaining what you think each piece of the sed script is doing.
The sed command actually works when entered into the command line. Here is how it breaks down:
Code:
'/browser[.]startup[.]homepage<snip>
Search for browser.startup.homepage
Code:
<snip>/auser_pref\("browser.startup.homepage", "'$homepg'"\)\;'
when found append user_pref("browser.startup.homepage", "$homepg"); after the line where the match was found.
Code:
'/browser[.]startup[.]homepage/d'
then delete the line with the match.

For some reason however, s2p does not like this syntax. Any ideas?

thanks again!
...drkstr
 
Old 07-13-2006, 02:28 PM   #7
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
Quick answer, more later when I can study it more.

Thanks for the great, responsive reply.

I missed the 'a' for "append" in trying read your original, it's not something I normally use. I'll try to figure out why the ',' is causing a problem. 1 possibility is to use a single 's' ("substitute") instead of 'a' + 'd'.
 
Old 07-13-2006, 03:13 PM   #8
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
Quote:
I'll try to figure out why the ',' is causing a problem. 1 possibility is to use a single 's' ("substitute") instead of 'a' + 'd'.
Thanks for takeing the time to help me, it's much appreciated! I considered using the 's' but I couldn't figure out how to do the following with it:
if { match partial string }
then { replace entire string }
Reading the documentation made me even more confused

Quote:
I missed the 'a' for "append" in trying read your original
Yeah, I like how the syntax just runs together with the text, it makes for very readable code.

I don't know if this will help, but to run the same sed command from a perl script using a system call, I had to escape the characters as follows (escaping the same way did not work with s2p for some reason):

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");
Unfortunately, the command didn't really work as I had hoped which is what brought me to abandoning sed altogether and learning how to do it in perl. Regardless, I would still like to know what I was doing wrong with s2p.

thanks again my friend!
...drkstr
 
Old 07-14-2006, 07:01 AM   #9
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
You're very welcome.

If you're going to use Perl, then learning to do it natively was a "Good Thing"(tm).

In my solution I'm assuming that you want to replace "http://www.drkstr.org" w/ "$homepg" & that it's ok to do it everywhere. If you want to change only 1 line, then you can add back the leading regex. (I put your test lines in the file "etc-wc_profile-prefs.js" together w/ some local identifying headers):
Code:
sed 's,http://www.drkstr.org,$homepg,' etc-wc_profile-prefs.js
Code:
# etc-wc_profile-prefs.js
# /home/ra/Documents/LQ/LQ-463177/etc-wc_profile-prefs.js
# test file for LQ thread 463177  (see LQ-463177.txt)
# =======================================================
user_pref("browser.link.open_newwindow.restriction", 0);
user_pref("browser.preferences.privacy.selectedTabIndex", 5);
user_pref("browser.search.selectedEngine", "Google");
user_pref("browser.startup.homepage", "$homepg");
user_pref("browser.tabs.warnOnClose", false);
user_pref("dom.disable_window_flip", true);
user_pref("dom.disable_window_move_resize", true);
Adding in the '-i' seems to work fine here, although normally I would save the orig. file & redirect the output to the new ver:
Code:
sed 's,http://www.drkstr.org,$homepg,' etc-wc_profile-prefs.js.000 >etc-wc_profile-prefs.js
Quote:
Originally Posted by drkstr
Reading the documentation made me even more confused
I have to agree that sed syntax isn't easy at 1st, heck sometimes it's never easy. If you have KDE, then using "info:sed" as a URL in Konqueror will give the info pages as HTML. http://www.gnu.org/software/sed/manu..._mono/sed.html is essentially the same info in 1 long web page -- you can search the whole thing w/ 1 request.

I have an old copy of sed & awk (O'Reilly), but I wouldn't stronly recommend it because the examples are all TeX based & meaningless to me. Also, the typeface is ugly & too small for my aging eyes.


Quote:
Originally Posted by drkstr
. . . I couldn't figure out how to do the following with it:
if { match partial string }
then { replace entire string }
Rather than try to guess where it's confusing you, feel free to ask how my code is working or any other sed Q's.
 
Old 07-15-2006, 02:08 AM   #10
drkstr
Senior Member
 
Registered: Feb 2006
Location: Seattle, WA: USA
Distribution: Slackware 11.0
Posts: 1,191

Original Poster
Rep: Reputation: 45
Thanks for the info. I'm starting to get a better understanding of how sed works.

I guess I didn't really explain what I was trying to do well enough. The sed script needs to be run multiple times and change the home page from an unknown address to the current address defined by $homepg. This is why I was searching for the string "browser.startup.homepage" since this is unique to the line, but will not change.

Now originally I was trying to get the $homepg variable to be expanded into the text so the actual text that get's replaced would be `echo $homepg`. However, after seeing the line your sed script produced, I realized I have been going about this completly wrong:
Code:
user_pref("browser.startup.homepage", "$homepg");
Instead of trying to change the text in the file, all I need to do is change the value of the $homepg and just reference it in the prefs.js file.

Unfortunatly, I got some realy weird result when I did this.
Code:
export homepg=http://www.google.com
when I started up firefox with the environment variable in my prefs.js file, I got this as the startup page
http://www.carlos.emory.edu/ODYSSEY/EGYPT/homepg.html

I wasn't expecting that one!

Everything I thought I knew just flew out the window.

THanks again for your help!
...drkstr

Last edited by drkstr; 07-15-2006 at 02:10 AM.
 
Old 07-15-2006, 12:45 PM   #11
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
This might have worked better:
Code:
export homepg='http://www.google.com'
Quoting is always a good idea when setting variables.
 
  


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
Help on sed and perl needed. cash_05 Programming 7 09-03-2005 04:33 PM
SED, AWK or PERL HELP embsupafly Programming 6 08-20-2005 09:07 PM
using sed with parentheses in perl beebop Linux - General 2 07-21-2005 10:41 PM
SED + perl !!! letmein Linux - General 9 10-07-2003 05:10 PM
perl with sed !!! letmein Linux - General 1 08-18-2003 05:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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