Suppose i have a file which contains 10 lines....
i have to delete a line which starts with the word "BEGIN"
and delete the consecutive lines till i find a start of line with the word "END"
how to do this?
sed -e "/^BEGIN/,/^END/d" in.txt > out.txt
Hi vino,
thatz gr8.this too works perfectly.
Actually my problem is something different.i thought i can get clue from this answer. but..
i wanted to remove the code present in the macros of a C program like...
#ifdef FLAG
//some code
//some code
#endif
if i give the argument as FLAG it shd remove all the code in that..
And the worst thing is this #ifdef can contain nested #ifdefs #else #elif all stuffs.
how can i match the correct #endif?CAn u give me some clue?
Regards,
Kavitha.
Removing code between macros' is not an easy task.
Try man cpp. cpp as in C Preprocessor That should help you out.
cpp -DFLAG -DANOTHER_FLAG -I/dir/with/header/files in.cpp out.cpp
Hi vino,
if u get time can u try this out? Execute it with ./scriptname <filename> <FLAG to be removed> as cmd line arguments
FLAG=0
FOUND=0
C="#ifdef"
T="${C} $2
exec 0<$1
set -f
while read -r $line
do
if [ "$line" = "$T" ] ;then
FLAG=1
FOUND=1
elif [ "$line" = "else" ] && [ $FOUND -eq 1 ] ;then
FLAG=0
elif [ "$(echo $line | awk '{print $1}')" = "#endif" ] && [ $FOUND -eq 1 ] ;then
FLAG=0
FOUND=0
elif [ $FLAG -ne 1 ]
echo $line
elif [ "$(echo $line |awk '{print $1}' )" = "#elif" ] ;then
echo $line | sed 's/elif defined/if/'
FLAG=0
FOUND=0
fi
done >$1.tmp
mv -f $1.tmp $1
REgards,
Kavitha
What is the problem that you are facing ?
Did you get a chance to try out cpp ?
that doesn't work vino.
now tell me...
i want to check if a word is present in a line,if present //do something...
like the macro can be #if or "ifdef" or "if DEFINED(FLAG)
if the line contains DEFINED(FLAG1) ,
then ///do something
if [ grep <here i want to use the FLAG given in cmd line> ]
since the C program contains ifdef statements like..
if defined(FLAG1) || defined(FLAG2)
Regards,
Kavitha