using sed(?) to delete a block of text


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using sed(?) to delete a block of text
# 1  
Old 10-21-2005
using sed(?) to delete a block of text

hello people,
i am trying to accomplish what i thought should be a simple task: find a token in a file and delete a number (let's say 25) of lines following the token.

in sed, i can't figure out how to do a relative address (i.e. something like /token/25dd to delete 25 lines) and in gnu grep, if i use the -A 25 option, it only works with a positive match, but not with -v

i suppose i could accomplish same by matching the number of open and closed parentheses (), since that's how the file is formatted, but i'm also not sure how to do that Smilie

any thoughts would be most welcome!

thanks in advance

ilya

P.S. for example

if the file contains:
===========
a: (
some text(more)
blah (blah)
)

b: (
another(word)
and stuff(gf)
)

===========

i would want to remove the 4 lines from a: to )
# 2  
Old 10-21-2005
GNU sed supports this.

from the manual page
Quote:
addr1,+N
Will match addr1 and the N lines following addr1.
So....

sed '/^a:/,+3 d' filename

should do what you want.

Cheers
ZB
# 3  
Old 10-21-2005
Using awk...
Code:
awk '/^a:/{c=4};--c<0' file

# 4  
Old 10-23-2005
your kung-fu is very strong!

thank you, thank you zazzybob and ygor!

In defence of my lack of RTFM: MANPATH on my solaris box! i got the solaris sed manpage, rather than the gnu one Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing a block of lines from a file, if that block does not contain two patterns using sed

I want to process a file block by block using sed, and if that block does not contain two patterns, then that complete block has to be printed. See below for the example data. ................................server 1............................... running process 1 running... (8 Replies)
Discussion started by: Kesavan
8 Replies

2. Shell Programming and Scripting

Block of text replacement using sed

Hi, I have a requirement in which i need to replace text as below - <stringProp name="Recipe">&lt;AddGroup Name=&quot;1001&quot; Path=&quot;ServiceAdministration/Controls/1001/ServiceSwitches&quot;&gt; &lt;Param Name=&quot;AttributeName&quot; Value=&quot;HeaderManipRspIngressRuleSet&quot; Type=&quot;String&quot; /&gt; &lt;Param Name=&quot;Value&quot;... (0 Replies)
Discussion started by: abhitanshu
0 Replies

3. Shell Programming and Scripting

Delete first block of text with sed/awk

Hello, guys! "filename" has blocks with three lines each in this fashion: 93909286 #verified has one bug 10909286 #unverified pending 10909286 #unverified pendingThe above example has duplicate blocks, and I have tried using sed to remove just one block... The... (2 Replies)
Discussion started by: teresaejunior
2 Replies

4. Shell Programming and Scripting

using sed/awk to replace a block of text in a file?

My apologies if this has been answered in a previous post. I've been doing a lot of searching, but I haven't been able to find what I was looking for. Specifically, I am wondering if I can utilize sed and/or awk to locate two strings in a file, and replace everything between those two strings... (12 Replies)
Discussion started by: kiddsupreme
12 Replies

5. Shell Programming and Scripting

Using tr, sed or awk to delete text from nth column only

Hi everyone, this is my first post here, I hope someone can help me. I have a file which I need to delete characters '_F3' from the end of the text in the first column. The problem is that the characters may also occur elsewhere in the file (i.e. second columns onwards). I tried sed (thinking I... (6 Replies)
Discussion started by: hlwright
6 Replies

6. Shell Programming and Scripting

Delete text between square brackets and also delete those square brackets using sed or awk

Hi All, I have a text file which looks like this: computer programming systems engineering I want to get rid of these square brackets and also the text that is inside these brackets. So that my final text file looks like this: computer programming systems engineering I am using... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

7. Shell Programming and Scripting

Delete block of text in one file based on list in another file

Hi all I currently use the following in shell. #!/bin/sh while read LINE do perl -i -ne "$/ = ''; print if !m'Using archive: ${LINE}'ms;" "datafile" done < "listfile" NOTE the single quote delimiters in the expression. It's highly likely the 'LINE' may very well have characters in it... (3 Replies)
Discussion started by: Festus Hagen
3 Replies

8. Shell Programming and Scripting

Filter duplicate block of text using SED

Hi, I would like to print a block of text between 2 regular expression using Sed, This can be achieved by using the command as shown below, however my problem is the same block of text is repeated twice. I would like to eliminate the duplicate block of text. For Example If my file... (5 Replies)
Discussion started by: dkumar91
5 Replies

9. Shell Programming and Scripting

Delete a block of text delimited by blank lines when pattern is found

I have a file which contains blocks of text - each block is a multi-lines text delimited by blank lines eg. <blank line> several lines of text ... pattern found on this line several more lines of text ... <blank line> How do you delete the block of text (including the blank lines) when... (17 Replies)
Discussion started by: gleu
17 Replies

10. Shell Programming and Scripting

Delete lines containing text with sed

hello all I have bunch of files containing lines of text that surrounding by <# .......#> tags I like to delete this lines from the text files whiteout open the files , can it be done with sed ? or other unix tool (perl mybe )? (2 Replies)
Discussion started by: umen
2 Replies
Login or Register to Ask a Question