Delete blocks of lines from text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete blocks of lines from text file
# 1  
Old 12-19-2007
Delete blocks of lines from text file

Hello,

Hello Firends,
I have file like below. I want to remove selected blocks say abc,pqr,lst. how can i remove those blocks from file.

zone abc {
blah
blah
blah }

zone xyz {
blah
blah
blah }

zone pqr {
blah
blah
blah }

zone lst {
blah
blah
blah }

zone bzs {
blah
blah
blah }
# 2  
Old 12-19-2007
if your sed has the -i option:
Code:
sed  -i  '/zone pqr/,/}/d' newfile

# 3  
Old 12-19-2007
Thanks Yogesh
I have almost 3000 blocks in this file. I have another list file which contain list of zone names e.g. pqr,abc,bzs.
I am using awk to run your sed command for each zone in list file

Code:
{
   zonename = $1
   sedcmd = "sed '/" zone zonename "/,/}/d' named.backup > named.backup.test"
   printf "Command: %s\n", sedcmd
   system(sedcmd)
   system("mv named.backup.test named.backup")
}

I think it's possible to optimize above coding. It's possible to remove system("mv named.backup.test named.backup") with some change.
Could you please help me for it
# 4  
Old 12-19-2007
have you checked if -i option is supported in your sed?
# 5  
Old 12-19-2007
awk

Hi,
Please try follow one.

One concern, i am not sure what will be content of all your blah blah. If they will cover all the content, maybe will cause some conflict with the code. Anyway, please try it, if any problem, please post it. Maybe all of us here can help you to solve it.

code:
Code:
sed '/^$/d' a > a.tmp
nawk 'BEGIN{
FS="\r"
RS="}\r"
a="abc"
b="pqr"
}
{
if ($1=="" && index($2,a)==0 && index($2,b)==0)
	print $0
if ($1!="" && index($1,a)==0 && index($1,b)==0)
	print $0
}' a.tmp
rm a.tmp

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tried many options but unable to delete blank lines from text file

Hi, I tried the following options but was unable to delete blank lines from file Input file = temp.hash.txt temp.hash.txt content 90 0 89.56 0 0 57575.4544 56.89 (9 Replies)
Discussion started by: uuuunnnn
9 Replies

2. Shell Programming and Scripting

Delete all CONSECUTIVE text lines from file shell scripting

Hi I have a text file like below. THe content of the text will vary. Entire text file have four consecutive lines followed with blank line. I want to delete the occurrence of the two consicutive lines in the text file. I don't have pattern to match and delete. Just i need to delete all... (5 Replies)
Discussion started by: RJSKR28
5 Replies

3. Shell Programming and Scripting

How to remove duplicate text blocks from a file?

Hi All I have a list of files which will have duplicate list of blocks of text. Following is a sample of the file, I have removed the sensitive information from the file. All the code samples starts from <TR BGCOLOR="white"> and Ends with IP address and two html tags like this. 10.14.22.22... (3 Replies)
Discussion started by: mahasona
3 Replies

4. Shell Programming and Scripting

How to delete lines of a text file based on another text file?

I have 2 TXT files with with 8 columns in them(tab separated). First file has 2000 entries whereas 2nd file has 300 entries. The first file has ALL the lines of second file. Now I need to remove those 300 lines (which are in both files) from first file so that first file's line count become... (2 Replies)
Discussion started by: prvnrk
2 Replies

5. Shell Programming and Scripting

How to delete lines from text file?

hi guys, I have very large txt files (200GB) and just want to to delete the first two lines (headers). So far I used sed -i '1,2d' infile.txtbut this command always takes extremely long as it writes all again. Is there a better way to do it (ie just to delete the lines without writing all... (2 Replies)
Discussion started by: TuAd
2 Replies

6. Shell Programming and Scripting

looking for a script that will delete lines in a text file

it will grep for a line and then delete these line. how do i begin to write this script if theres no available one? (3 Replies)
Discussion started by: garfish
3 Replies

7. UNIX for Dummies Questions & Answers

Delete vertical lines in an text file

Hi everybody! I need to delete several vertical lines in a huge text file. It should work like the example below. Delete the vertical lines 2 and 8. 123456789 masldfjla afsajfwel sajfljsaf safsarfrl sajfeljwq 1345679 msldfja asajfwl sjfljsf sfsarfl sjfeljq Is there a... (11 Replies)
Discussion started by: relaxo
11 Replies

8. Shell Programming and Scripting

extract blocks of text from a file

Hi, This is part of a large text file I need to separate out. I'd like some help to build a shell script that will extract the text between sets of dashed lines, write that to a new file using the whole or part of the first text string as the new file name, then move on to the next one and... (7 Replies)
Discussion started by: cajunfries
7 Replies

9. Shell Programming and Scripting

Delete Blank Lines Between DHCP Host Blocks

Hi All, I have a dhcpd.conf file that gets static hosts added and removed via a shell script. After sometime, there becomes huge gaps of space ( blank lines ) between each host block. I tried a couple of sed one-liners; but, I can't seem to get the output I'm looking for. Also, I would like... (4 Replies)
Discussion started by: cstovall
4 Replies

10. Programming

Delete specific lines in a text file

Hi, experts, I would like to create a function that can calculate the total number of lines in a saved text file and delete specific lines in that particular file (I only want the last few lines). Hav anybody have the experience and giv me a hand in this? (9 Replies)
Discussion started by: dniz
9 Replies
Login or Register to Ask a Question