I am facing problem with replacing one of the metacharacter i.e. '{'.
I am searching for a pattern::
#ifdef ABC
{
abclkckl();
}
This is a typical pattern in .c file.
and want to replace it with::
#ifdef ABC
#ifdef XY
tutu();
#endif
{
abclkckl();
}
#ifdef XY
tutu1();
#endif
#endif
Here the original pattern is as it is, and we can replace "{" with #ifdef XY tutu(); #endif
I started my quest with::
$start = "#ifdef XY\ntutu\(\)\;\n#endif\n";
s/(#ifdef\s+ABC)\s+/$1\n$start/g;
*******************
With this i was able to get:
#ifdef ABC
#ifdef XY
tutu();
#endif
{
abclkckl();
}
#endif
*************************
I was not able to replace '{' meta character.
I tried using
s/(#ifdef\s+ABC)\s+\{/$1\n$start/g;
as \ can escape metacharacters, i.e. it makes them behave as literals, so i used this sequence.
Help me out
Thanks in advance.
my pattern code was
$/=""; # Make it take input in paragraph
And this was my problem, i was parsing the input file line by line.
If i take input in paragraph then the code below gave me perfect results
s/(#ifdef\s+ABC)\s+(\{)/$1\n$start$2/g;
Thanks anyways.
For the search and replace pattern i mentioned above, i have one doubt.
I have a pattern::
#ifdef do_dod
{
try((unsigned int)(&(Array1[0])), (unsigned int)(&(Array2[0])), (unsigned int)(&(Array3[0])));
}
#else
for(j=0;j<LEN;j++)
{
Array3[j] = Array1[j] + Array2[j];
Array1[j] = Array2[j] + Array3[j];
}
#endif
I was able to replace it with ::::
$Call = "try((.*";
$Hello = "my hello";
$Hello1 = "my hello1";
$_ =~ s/(#ifdef\s+do_dod)\s+(\{)\s+($Call)\s+(\})\s+(#else)(\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*))/$1\n$Hello\n$2\n\n$3\n\n$Hello1\n$4\n\n$5\n\n$Hello\n\n$6\n\n$Hello1\n\n/g;
#ifdef do_dod
my hello
{
try((unsigned int)(&(Array1[0])), (unsigned int)(&(Array2[0])), (unsigned int)(&(Array3[0])));
}
my hello1
#else
my hello
for(j=0;j<LEN;j++)
{
Array3[j] = Array1[j] + Array2[j];
Array1[j] = Array2[j] + Array3[j];
}
my hello1
#endif
************************
But the problem here is:::
\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*)\s+(.*))
Can we have anything else for this recurring pattern.
One more problem is , we don't know how many times it will comes.
This pattern is for
*********************
sdfkljsdljsdfldj
sdfl;jsljks
lsjdflsjkdl
***********************
i.e. to detect each line and we don't know how many lines will be present.
Thanks in advance
With this i am able to get desired result of::