LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2010, 06:28 PM   #1
dougp23
Member
 
Registered: Oct 2006
Posts: 43

Rep: Reputation: 15
Substitute Text in Many Files


I have this PHP Program I have been hobbying for years. Started in old PHP3 early PHP4. So with the newer PHP, things are starting to break!

I have this string:

session_is_registered('UserID')

And I need to convert to this:

isset($_SESSION['UserID'])

in more than 200 scripts in my directory. I have been toying with perl (in a copy of the dir of course in case I really screw things over, lol) but am not gettig too far. The new code needs to swap ( ) for [ ] and i'm just all hung up!

I am getting lost in Perl, I have this, but it does not substitute so I am missing something:

perl -i.bak -pe "s/session_is_registered\(\'UserID\'\)/isset\(\\\$_SESSION\[\'UserID\'\]\)/g" *.php

Would appreciate any help! (Even if you think something is better than Perl at this!)
 
Old 03-11-2010, 06:31 PM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 97
Code:
perl -i.bak -pe "s/session_is_registered\('UserID'\)/isset\(\\\$_SESSION\['UserID'\]\)/g" *.php
is working for me.
 
Old 03-11-2010, 06:38 PM   #3
dougp23
Member
 
Registered: Oct 2006
Posts: 43

Original Poster
Rep: Reputation: 15
Still not working for me...

Does it matter that the line of code begins with a !:

if(!session_is_registered('UserId') || $_SESSION['UserType'] != "A")
 
Old 03-11-2010, 06:46 PM   #4
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 97
Code:
perl -i.bak -pe "s/session_is_registered\('UserID'\)/isset\(\\\$_SESSION\['UserID'\]\)/gi" *.php
 
1 members found this post helpful.
Old 03-11-2010, 06:49 PM   #5
dougp23
Member
 
Registered: Oct 2006
Posts: 43

Original Poster
Rep: Reputation: 15
Thank you sssoooo much! I guess the i after the g was the hangup! I had tried at least 15 different ways!

Thanks again.
 
Old 03-11-2010, 06:56 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Code:
$ cat php.sed 
s/session_is_registered('UserID')/isset($_SESSION['UserID'])/g
$ cat 2test.php test.php
session_is_registered('UserID')
session_is_registered('UserID')
$ sed -i -f php.sed 2test.php 
$ cat 2test.php test.php
isset($_SESSION['UserID'])
isset($_SESSION['UserID'])
$
 
1 members found this post helpful.
Old 03-11-2010, 07:00 PM   #7
dougp23
Member
 
Registered: Oct 2006
Posts: 43

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Tinkster View Post
Code:
$ cat php.sed 
s/session_is_registered('UserID')/isset($_SESSION['UserID'])/g
$ cat 2test.php test.php
session_is_registered('UserID')
session_is_registered('UserID')
$ sed -i -f php.sed 2test.php 
$ cat 2test.php test.php
isset($_SESSION['UserID'])
isset($_SESSION['UserID'])
$
Very interesting. I had been trying sed as well...but this just looks so simple, I'm not sure how I missed it.
 
Old 03-11-2010, 07:02 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by dougp23 View Post
Very interesting. I had been trying sed as well...but this just looks so simple, I'm not sure how I missed it.
The 'trick' is to stick the pattern(s) into a file;
saves you having to escape "pesky" things like ' ... ;}


Cheers,
Tink
 
  


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
join 2 text files based on first number present in every line of the 2 text files markraem Linux - Software 4 01-25-2010 06:26 AM
[SOLVED] BASH: how to substitute just one occurency of a pattern in a text file carolflb Linux - Newbie 8 11-19-2009 03:31 AM
[SOLVED] BASH - how to substitute many lines in a text file at once carolflb Linux - Newbie 6 10-20-2009 01:28 PM
[SOLVED] BASH - how to substitute many lines in a text file at once carolflb Programming 2 10-20-2009 10:21 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:14 PM.

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