Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Find replace : Regular expression

Reply
Thread Tools

Find replace : Regular expression

 
 
Subra
Guest
Posts: n/a
 
      06-12-2007
Hi,

I have to replace the strings "LINUX", "__linux__", "linux" with
"__linux". Please help me to correct the below script.

My input file is of type below

#ifdef LINUX
DO SOME THING
#end if

#ifdef __linux
DO SOME THING
#endif

#ifdef linux
DO SOME THING
#endif

#ifdef __linux__
DO SOME THING
#endif


My o/p file should be

#ifdef __linux
DO SOME THING
#end if

#ifdef __linux
DO SOME THING
#endif

#ifdef __linux
DO SOME THING
#endif

#ifdef __linux
DO SOME THING
#endif

My script (Which needs correction)
#!/tools/opt/bin/perl
$fileCount = 0;
while($fileCount <= @ARGV-1)
{
open(FileHandle,$ARGV[$fileCount]) || die ("File not present
$ARGV[$fileCount]");
open(OpFile,">OpFile.$ARGV[$fileCount]") || die ("OpFile not
present");
while($line=<FileHandle>)
{
#$line=~s/(^#.*)LINUX(.*)/$1__linux$2/g;
$line=~s/(^#.*)linux(.*)/$1__linux$2/g;
#$line=~s/(^#.*)__linux__(.*)/$1__linux$2/g;

print OpFile $line;
}
close(FileHandle);
$fileCount++;
}

 
Reply With Quote
 
 
 
 
Subra
Guest
Posts: n/a
 
      06-12-2007
I think the below script works
$fileCount = 0;
while($fileCount <= @ARGV-1)
{
open(FileHandle,$ARGV[$fileCount]) || die ("File not present
$ARGV[$fileCount]");
open(OpFile,">OpFile.$ARGV[$fileCount]") || die ("OpFile not
present");
while($line=<FileHandle>)
{
$line=~s/(^#.*)__linux__(.*)/$1__linux$2/g;
$line=~s/(^#.*)LINUX(.*)/$1__linux$2/g;
$line=~s/(^#.*)[\s]+linux(.*)/$1 __linux $2/g;

print OpFile $line;
}
close(FileHandle);
$fileCount++;
}

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-12-2007
Subra wrote:
>
> $line=~s/(^#.*)__linux__(.*)/$1__linux$2/g;
> $line=~s/(^#.*)LINUX(.*)/$1__linux$2/g;
> $line=~s/(^#.*)[\s]+linux(.*)/$1 __linux $2/g;


That could possibly be written:

$line =~ s/(^#.*?)(?:__)?linux(?:__)?/$1__linux/gi;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
Skye Shaw!@#$
Guest
Posts: n/a
 
      06-12-2007
On Jun 11, 10:41 pm, Subra <(E-Mail Removed)> wrote:
> Hi,
>
> I have to replace the strings "LINUX", "__linux__", "linux" with
> "__linux". Please help me to correct the below script.


Maybe try this:

[sshaw@localhost ruby]$ perl -i.og -pe'/^#ifdef/ && s/(?:__)?
linux(?:__)?/__linux__/i' file1 file2

file1, file2 will contain the changes. The original files will be save
with a .og extension

I say /^#ifdef/ just in case you are really using `perl -P` on your
source files, <:^|
On another note, has anyone ever used the preprocessor with their perl
programs?

 
Reply With Quote
 
Ala Qumsieh
Guest
Posts: n/a
 
      06-12-2007
Gunnar Hjalmarsson wrote:

> Subra wrote:
>>
>> $line=~s/(^#.*)__linux__(.*)/$1__linux$2/g;
>> $line=~s/(^#.*)LINUX(.*)/$1__linux$2/g;
>> $line=~s/(^#.*)[\s]+linux(.*)/$1 __linux $2/g;

>
> That could possibly be written:
>
> $line =~ s/(^#.*?)(?:__)?linux(?:__)?/$1__linux/gi;


you meant this:

$line =~ s/(^#.*?)(?:__)?linux(?:__)?/${1}__linux/gi;

the OP had it wrong too, and I suspect you just copied/pasted.

--Ala

 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-13-2007
Ala Qumsieh wrote:
> Gunnar Hjalmarsson wrote:
>> Subra wrote:
>>> $line=~s/(^#.*)__linux__(.*)/$1__linux$2/g;
>>> $line=~s/(^#.*)LINUX(.*)/$1__linux$2/g;
>>> $line=~s/(^#.*)[\s]+linux(.*)/$1 __linux $2/g;

>> That could possibly be written:
>>
>> $line =~ s/(^#.*?)(?:__)?linux(?:__)?/$1__linux/gi;

>
> you meant this:
>
> $line =~ s/(^#.*?)(?:__)?linux(?:__)?/${1}__linux/gi;
>
> the OP had it wrong too, and I suspect you just copied/pasted.


Well, no, to be honest I didn't think of it, but I had tested the code
before posting.

Now, when I play with various alternatives, it seems as if the braces
are only needed if the string following the dollar-digit variable begins
with a digit.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find and replace in a file with regular expression TOXiC Python 3 02-03-2007 12:37 PM
Search, Replace with Regular Expression tushar.n.patel@gmail.com ASP .Net 1 02-09-2005 10:09 PM
Search, Replace with Regular Expression lucky ASP .Net 0 02-09-2005 07:10 AM
regular expression search replace J. VerSchave Java 2 02-29-2004 08:08 PM
Dynamically changing the regular expression of Regular Expression validator VSK ASP .Net 2 08-24-2003 02:47 PM



Advertisments