The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
cut a part of the file kurosaki Shell Programming and Scripting 1 06-02-2008 06:14 AM
grep within certain part of file Orbix UNIX for Dummies Questions & Answers 2 11-26-2007 03:45 AM
renaming part of a file ajaya Shell Programming and Scripting 0 04-11-2006 07:00 PM
Add Date as part of file name amsh76 Shell Programming and Scripting 3 09-05-2005 06:18 PM
Help with multiple file rename - change case of part of file name steve7 UNIX for Dummies Questions & Answers 7 06-30-2005 10:41 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-20-2006
Registered User
 

Join Date: Jun 2006
Posts: 8
How to delete part of a file?

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?
Reply With Quote
Forum Sponsor
  #2  
Old 06-20-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,698
Code:
sed -e "/^BEGIN/,/^END/d" in.txt > out.txt
Reply With Quote
  #3  
Old 06-21-2006
Registered User
 

Join Date: Jun 2006
Posts: 8
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.
Reply With Quote
  #4  
Old 06-21-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,698
Removing code between macros' is not an easy task.

Try man cpp. cpp as in C Preprocessor That should help you out.

Code:
cpp -DFLAG -DANOTHER_FLAG -I/dir/with/header/files in.cpp out.cpp
Reply With Quote
  #5  
Old 06-21-2006
Registered User
 

Join Date: Jun 2006
Posts: 8
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
Reply With Quote
  #6  
Old 06-21-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,698
What is the problem that you are facing ?

Did you get a chance to try out cpp ?
Reply With Quote
  #7  
Old 06-21-2006
Registered User
 

Join Date: Jun 2006
Posts: 8
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:54 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0