Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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 doing a search a replace using regex in PERL. I am reading the search and replace string from a file storing in a variable and trying to replace it.
$srcstr='fopen\((\w+)\,(\w+)\)'; #read from a file
$deststr='fopen\($2\,$1)'; #read from a file
$line =~ s/$srcstr/$deststr/;
This search and replace doesnt work , when we use capturing and backreference as a variable in Perl.
You may have to wait for one of the perl experts, but I think your issue is one of timing.
At the point where you set deststr your positional items have no values and hence the string is set literally.
However your srcstr is interpreted once inside the search, so it is grabbing all that matched and then changing it for the literal string.
There may be some perl magic one of the gurus knows or a simple step you need to include, so hopefully they will jump on and advise
I would check some of your escapes in the deststr as well as there are either some missing or too many in the first place.
The problem is one of substitution. What you are trying to do is a double interpretation of the "$deststr". First to replace the variable with the string... then again to replace the back references.
The "s" operator does only single substitution, like all handling of " strings. The first parameter is then interpreted (again), but the second is not - it is presumed to be a constant.
This is not necessarily the best way as it requires the $line, $srcstr and $deststr to be global.
Another way (which is more likely to be what you want) uses the "e" modifier (twice). This also requires a slight change in the specification of the value of $deststr:
Note the value of $deststr - it now has embedded the evaluation with a quoted string( a '"..."'). This is necessary because the e modifier causes the value to evaluated - causing string substitution which handles the $1 and $2 substitutions, then evaluates that (which removes the "..."), which is then used for the replacement string.
If the " quoting in $deststr is missing, then it will evaluate the string as the perl expression fopen(filehandle,fname), which would return nothing (usually, as the fname just might not exist). With the quotes the evaluation returns the string you want.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.