I need to search for a particular function in a shell script and replace it with the modified function for which I can use sed inplace editor. But I am not able to correctly parse for the function. Can I have a regular expression to search for the function which is of the form
function()
{
no other braces in between
}
I already showed you how to do this, Elizabeth. Here is a more complete example demonstrating how to use what I showed you to more specifically add statements to the end of a specific function: -
use strict;
-
use warnings;
-
-
my $data = do {local $/; <DATA>};
-
-
$data =~ s~(bar\(\)\s*({(?:(?>[^{}]+)|(?-1))*}))~
-
my $func = $1;
-
-
# Remove last }, add it back later
-
chop $func;
-
-
# Add new statements to end, including trailing }
-
$func .= "
-
# This is just a trailing statement
-
}";
-
-
$func
-
~ex;
-
-
print $data;
-
-
1;
-
-
__DATA__
-
foo() {
-
# Fake stuff
-
}
-
-
bar() {
-
if (1) {
-
-
}
-
-
for () {
-
if (0) {
-
if (1) {
-
-
}
-
}
-
}
-
}
-
-
baz() {
-
# Fake stuff
-
}
-
- Miller
15 2691
If there's no braces that makes it a lot easier.
Try function[^{]*{[^}]*}
I am sorry but it does have some braces inside the function.. so I dont know how to parse until the end of function
Try what @Rabbit put in above? You search from the beginning of the function with the opening bracket until the closing bracket. Did you try what he posted? Did it work?
In perldoc perlre, search for the heading (?PARNO).
This may be too advanced for you, but the below code parses out the bar function from the DATA block. -
my $data = do {local $/; <DATA>};
-
-
my $brackets_re = qr{
-
(
-
\{
-
(?:
-
(?> [^{}]+ )
-
|
-
(?-1)
-
)*
-
\}
-
)
-
}sx;
-
-
$data =~ s/(bar\(\) $brackets_re)//;
-
-
print $data;
-
-
1;
-
-
__DATA__
-
foo() {
-
# Fake stuff
-
}
-
-
bar() {
-
if (1) {
-
-
}
-
-
for () {
-
if (0) {
-
if (1) {
-
-
}
-
}
-
}
-
}
-
-
baz() {
-
# Fake stuff
-
}
-
But in this particular case, the opening brace and the closing brace for the function body always are in the beginning of the line, aren't they?
I tried what @rabbit mentioned , but didnt work. Guess it was coz I said the function doesnt have any braces inside it.
This doesn't really answer my question.... Because when the function braces are only in column 1, and the other braces are not in column 1, you can use this to distinguish them, by just searching for braces at the beginning of a line.
Will you be able to still help me with the above question. I am still not able to parse the function correctly
???
What exactly was unclear with my last response from March 21?
Thanks for the quick response. Like you had mentioned above, function braces are only in column 1, and the other braces are not in column 1 but not sure how to do the search I need to insert a new statement just before the closing curly brace of the function. Is function parsing the best way to do this or do I have any other option?
If you sure that *all* your function's closing curlies are in column 1, and no other curly brace is in column 1, and you want to insert something just before the function's closing curly brace, things are trivial. You just read your file line by line, and whenever you encounter the closing of a function, you insert your new statement.
However this solution is so obvious, that I there must be some hidden problem I don't understand yet. Did I overlook something?
I already showed you how to do this, Elizabeth. Here is a more complete example demonstrating how to use what I showed you to more specifically add statements to the end of a specific function: -
use strict;
-
use warnings;
-
-
my $data = do {local $/; <DATA>};
-
-
$data =~ s~(bar\(\)\s*({(?:(?>[^{}]+)|(?-1))*}))~
-
my $func = $1;
-
-
# Remove last }, add it back later
-
chop $func;
-
-
# Add new statements to end, including trailing }
-
$func .= "
-
# This is just a trailing statement
-
}";
-
-
$func
-
~ex;
-
-
print $data;
-
-
1;
-
-
__DATA__
-
foo() {
-
# Fake stuff
-
}
-
-
bar() {
-
if (1) {
-
-
}
-
-
for () {
-
if (0) {
-
if (1) {
-
-
}
-
}
-
}
-
}
-
-
baz() {
-
# Fake stuff
-
}
-
- Miller
Thnks alot..it wrks wellll....I dnt speak Perl much..so not much sure on how to modify.. can I have it in way where I can use this .pl file to call different files..and apply the same changes to all files(ie, parse out the specific fn in all files and add a trailing statement to all)? Again thnks for ya help!
@ROVF: I was told that perl might be a good way to do this...Since i dnt speak Perl much, it takes me more time to implement even the basic things, but you had got it right...I guess I was thinking it a bit too complex...Thnks again for ya quick responses and ya help
If you don't know Perl well, it would already help if you sketch the algorithm you want to implement in pseudo-code (or in any other programming language you are used to), and point out which part you are having problems implementing in Perl. We can provide much better help if we know your level of knowledge.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Tito |
last post by:
From the Python's tutorial, about default argument values:
<quote>
The default value is evaluated only once. This makes a difference when the
default is a mutable object such as a list,...
|
by: muser |
last post by:
The following error appears for the code I'm writing:
error C2062: type 'int' unexpected
C:\Program Files\Microsoft Visual
Studio\MyProjects\Valid\Zenith124\Zenith.cpp(427) : error C2143:...
|
by: jack |
last post by:
Hi there,
I have a short question about template function
definition.
1) The following files will not compile in M$ VC++
but will have no problem with g++.
2) However, if I uncomment all...
|
by: cesco |
last post by:
Hi,
I need to define the body of a function in the constructor of a class.
For example, the class has a member function Log (that is the public
interface of the class), but I want the body of...
|
by: rajesh6695 |
last post by:
Hai
Where the pre defined function definitions and declarations will be available either in the header file or in the library file...
I think it will be in the library files i am not sure so...
|
by: djhong |
last post by:
Hi,
BSD code has following hcreate function definition in hsearch.c
under lib/libc/db/hash/hsearch.c.
Here, why is 'extern' used ? What's the purpose of it? How come
in infront of...
|
by: Old Wolf |
last post by:
I have some code that has in the header file:
void foo( char bar );
and in the source file:
void foo( bar )
char bar;
{ /* etc. */ }
The compiler (with many warnings enabled) warns that...
|
by: MarQade |
last post by:
Hi, I am new here. I am also a first-year computer science student looking for some help with a very small C++ program.
This is a 3-file program, header file with declarations and 2 source files,...
|
by: aryan |
last post by:
Is it not allowed to have template function definition in a .cc file?
Here is the scenario. The tempage function declaration is in a header
file and the definition in a .cc file. The function is...
|
by: JRough |
last post by:
I have a function definition error:
Warning: Missing argument 2 for makexcldata(), called on line 123 and
defined in on line 149
"Warning: mysql_fetch_row(): supplied argument is not a valid...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |