Find replace : Regular expression

S

Subra

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++;
}
 
S

Subra

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++;
}
 
G

Gunnar Hjalmarsson

Subra said:
$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;
 
S

Skye Shaw!@#$

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?
 
A

Ala Qumsieh

Gunnar said:
Subra said:
$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
 
G

Gunnar Hjalmarsson

Ala said:
Gunnar said:
Subra said:
$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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top