I am having file xyz.log
Its content is like this
int main()
{
d;
#ifdef
e;
f;
#else
g;
#ifdef
h.
#else
i;
RET_OK;
}
-------------------------------
I need to delete the lines starting from
#ifdef to #else
when i am trying this command
s/#ifdef(.|n)*#else//;
It is deleting like this
--------------
int main ();
{
d;
i;
RET_OK;
}
-------------
It is deleting "g" also
It is searching from #ifdef until the last #else , and replacing.
I need to delete from #ifdef to the nearest match "#else"
How to do that
what will be the best script command in perl to do this
The range expression of man awk (linux)
The pattern1, pattern2 form of an expression is called a range pattern. It matches all input records starting with a record that matches pattern1, and continuing until a record that matches pattern2, inclusive.
may be a better choice. If you have:
awk '/^#ifdef/,/^#else/ { print; }' filename
you will get:
#ifdef
e;
f;
#else
#ifdef
h.
#else
which is the lines you want to remove. You cannot negate the pattern:
awk '! /^#ifdef/,/^#else/ { print; }' filename
instead, you have to do something like:
awk '{ f = 1; } /^#ifdef/,/^#else/ { f = 0; } f { print; }' filename
which will print the lines not inside the pattern.
Hope this helps.
The Perl way.
perl -i.bak -pe '(/^#ifdef/../^#else/)&&s/$_//g' inputfile
Also, this one-liner will create a backup of the original input file with '.bak' in its name.