How to delete part of a file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to delete part of a file?
# 1  
Old 06-21-2006
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?
# 2  
Old 06-21-2006
Code:
sed -e "/^BEGIN/,/^END/d" in.txt > out.txt

# 3  
Old 06-21-2006
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.
# 4  
Old 06-21-2006
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

# 5  
Old 06-21-2006
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
# 6  
Old 06-21-2006
What is the problem that you are facing ?

Did you get a chance to try out cpp ?
# 7  
Old 06-21-2006
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to make a loop to read the input from a file part by part?

Hi All, We've a VDI infrastructure in AWS (AWS workspaces) and we're planning to automate the process of provisioning workspaces. Instead of going to GUI console, and launching workspaces by selecting individual users is little time consuming. Thus, I want to create them in bunches from AWS CLI... (6 Replies)
Discussion started by: arun_adm
6 Replies

2. Shell Programming and Scripting

To Delete the duplicates using Part of File Name

I am using the below script to delete duplicate files but it is not working for directories with more than 10k files "Argument is too long" is getting for ls -t. Tried to replace ls -t with find . -type f \( -iname "*.xml" \) -printf '%T@ %p\n' | sort -rg | sed -r 's/* //' | awk... (8 Replies)
Discussion started by: gold2k8
8 Replies

3. Shell Programming and Scripting

Delete part of a column

I want to delete a part of the 4th column from the given file below: <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text Text_ID="10155645315851111_10155645317023456"... (2 Replies)
Discussion started by: my_Perl
2 Replies

4. Shell Programming and Scripting

Find and delete part of field with awk or sed

I've got a file that looks like this (the whitespace between commas is intentional): 123456789,12,JOHN H DOE ,DOE/JOHN H ,,,DOE/JOHN H ,,,,,123 FAKE STREET ,SPRINGFIELD,XX, I want to strip just the first name out of the third field so it reads "JOHN,". So far I... (6 Replies)
Discussion started by: Scottie1954
6 Replies

5. Red Hat

Need Script to ZIP/SAVE & then DELETE Log file & DELETE ZIPS older than 12 months

ENVIROMENT Linux: Fedora Core release 1 (Yarrow) iPlanet: iPlanet-WebServer-Enterprise/6.0SP1 Log Path: /usr/iplanet/servers/https-company/logs I have iPlanet log rotation enabled rotating files on a daily basis. The rotated logs are NOT compressed & are taking up too much space. I... (7 Replies)
Discussion started by: zachs
7 Replies

6. Shell Programming and Scripting

[Solved] Printing a part of the last line of the specific part of a file

Hi, I have 80 large files, from which I want to get a specific value to run a Bash script. Firstly, I want to get the part of a file which contains this: Name =A xxxxxx yyyyyy zzzzzz aaaaaa bbbbbb Value = 57 This is necessary because in a file there are written more lines which... (6 Replies)
Discussion started by: wenclu
6 Replies

7. Shell Programming and Scripting

Delete Last part

Hello, I am on solaris (ksh). I have a log file like this with directory structure and file name : /DIR1/DIR2/DIR3/filename /DIR1/DIR2/filename /DIR1/DIR2/DIR3/DIR4/DIR5/filename I am looking for a way to create a new file to be formated like this : DIR2/DIR3 DIR2... (9 Replies)
Discussion started by: Aswex
9 Replies

8. Shell Programming and Scripting

delete only part of a line

Hi, I've a file as below- I need to delete only the part of the line after the pattern 'extent /lock' and my output has to be like below. Thanks (7 Replies)
Discussion started by: dvah
7 Replies

9. Shell Programming and Scripting

delete part of file from pattern1 to pattern2

Hi all! How can I delete all the text starting from <string1> to <string2> in all the .txt files of the folder "FOLDER" ? Thanks a lot! mjomba ... </s> <s> <w></w> </s> <s> ... to get: (1 Reply)
Discussion started by: mjomba
1 Replies

10. Shell Programming and Scripting

Delete part of string

This command: du -s /Applications/TextMate.app Returns an output like this: 65792 /Applications/TextMate.app I need to delete the space and the file path in the output leaving just the number. Thanks (2 Replies)
Discussion started by: pcwiz
2 Replies
Login or Register to Ask a Question