Results 1 to 7 of 7
So I often find myself needing to do a search and replace throughout an entire linux folder. Thanks To Google, I usually use a command such as the following which ...
- 06-28-2009 #1Just Joined!
- Join Date
- Jun 2009
- Posts
- 4
Less Specific Search and Replace, multiple files - Perl
So I often find myself needing to do a search and replace throughout an entire linux folder. Thanks To Google, I usually use a command such as the following which has always met my needs.
perl -pi -w -e 's/search/replace/g;' *.php
But right now I am needing to do something less specific and a little more difficult.
I want to search through an entire folder for all my PHP $_SESSION variables such as
$_SESSION['layout']
$_SESSION['userid']
etc.
and many more. All I really want to do is add the number 2 to all the variable names within the $_SESSION array so you would have
$_SESSION['layout2']
$_SESSION['userid2']
etc.
I dont suspect this is too difficult but special expressions are not my strong point. Can someone help me out? Much appreciated.Last edited by UCCC; 06-28-2009 at 06:19 PM. Reason: Clarifying Title.
- 06-29-2009 #2
So I'm not great on Perl one-liners, but the regular expression here is fairly simple:
What this does is to "capture" the actual text that appears between the ['...']. This is what the parentheses mean. This capture is stored into the special variable $1, which we can then use in the replacement text.Code:s/\$_SESSION\['(\w+)'\]/\$_SESSION['${1}2']/
Does this make sense?DISTRO=Arch
Registered Linux User #388732
- 06-29-2009 #3Linux User
- Join Date
- Aug 2006
- Posts
- 458
only if your SESSION statements are on one line each, here's a snippet using find/gawk
Code:find /path/to/search -type f -name "*.php" -printf "%p\n" | gawk -F":" ' { while( ( getline line < $1)> 0 ){ if ( line ~ /\$_SESSION/){ b = gensub( /(.*\$_SESSION\[\047)(.*)(\047\])/, "\\1\\22\\3","g",line) line=b } print line > "temp" } cmd ="mv temp \047"$1"\047" #system(cmd) #uncomment to use } '
- 06-29-2009 #4Just Joined!
- Join Date
- Jun 2009
- Posts
- 4
Thanks to both of you, have a great week.
- 08-01-2009 #5Just Joined!
- Join Date
- Jun 2009
- Posts
- 4
I thought this ended up being a non issue but I was wrong. Unfortunately I am struggling with both the suggestions in this thread.
When I use the Perl statment combined with Cabhan's Special Expression, I am getting an error message saying
-bash: syntax error near unexpected token `('
I try to Escape the ( and ) but that doesnt work. It runs, but produces no results.
When I try the code ghostdog74 write, and run it from a command prompt, it runs, but again there are no changes to the PHP file I am using to test.
Anyone out there who might help me today? Thanks
- 08-01-2009 #6
You're going to have to do a lot of weird Bash escaping. It's one reason I don't like one-liners
.
The regular expression I gave, fully escaped for Bash (assuming that you are surrounding it in double-quotes), is:
It's annoying, but there you go. Note that I haven't actually tested this, but I believe it's correct.Code:"s/\\\$_SESSION\\['(\\w+)'\\]/\\\$_SESSION['\$\{1\}2'\]/"DISTRO=Arch
Registered Linux User #388732
- 08-01-2009 #7Just Joined!
- Join Date
- Jun 2009
- Posts
- 4
Cabhan - Thanks again, very much appreciated. I had just launched the site and had thousands of visitors excited and this session problem was a major problem! I was freaking out!
Whenever I find myself stuck in a situation, I find its best to find more simple solutions. So in the end, that is what I did. I asked myself, Why do I need to put the 2 at the end of the variable name? Why can't I just put it at the beginning. Much simpler. I was revealed.
Problem is definitely 100% solved. Thanks again.


Reply With Quote