Linux - SoftwareThis 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
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.
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':
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**
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
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)
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'.
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):
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.