Delete first block of text with sed/awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete first block of text with sed/awk
# 1  
Old 01-19-2012
Java Delete first block of text with sed/awk

Hello, guys!

"filename" has blocks with three lines each in this fashion:
Code:
93909286
    #verified
    has one bug
10909286
    #unverified
    pending
10909286
     #unverified
     pending

The above example has duplicate blocks, and I have tried using sed to remove just one block... The following piece of code removes all matching blocks, but I want to have only the duplicates removed:
Code:
sed -i '/^10909286$/,+2 d' filename

The following awk code has the same problem as the sed one, besides not being able to edit the file inline as "sed -i", but I could workaround it:
Code:
awk '/^10909286$/{c=3};--c<0' filename

Any ideas?

Thank you!

Last edited by teresaejunior; 01-19-2012 at 07:16 AM.. Reason: bananas don't have wings
# 2  
Old 01-19-2012
Try:
Code:
awk '!a[$0]&&/^[0-9]/{a[$0]=1;print;getline;print;getline;print}' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 01-19-2012
Quote:
Originally Posted by bartus11
Try:
Code:
awk '!a[$0]&&/^[0-9]/{a[$0]=1;print;getline;print;getline;print}' file

Hello, bartus, thanks!

I think I'm almost there: the file's got 3792 lines; with no duplicates (using sort -u | wc -l) it has 3378; and with your awk command it has 3384. So I think there are still two blocks remaining... Could you please explain a bit your code?

---------- Post updated at 09:38 AM ---------- Previous update was at 09:23 AM ----------

There are no errors in your code, thank you! There are duplicate lines, but no duplicate blocks!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete a character using sed and or awk?

Hi, 1/ i have file test.txt 1 Jul 28 08:35:29 2014-07-28 Root::UserA 1 Jul 28 08:36:44 2014-07-28 Root::UserB i want to delete the seconds of the file, and the Root:: and the output will be: 1 Jul 28 08:35 2014-07-28 UserA 1 Jul 28 08:36 2014-07-28 UserB 2/i have another file test2.txt:... (8 Replies)
Discussion started by: fxsme
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

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

4. 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

5. 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

6. Shell Programming and Scripting

Delete line with sed or awk (with specified condition)

Hello. I'm trying to delete the lines of a file does not contain the letter "T " (for example) at position 26. So far, I could only print the result: awk '{if (substr ($ 1,1,26)! ~ / T /) print}' file.txt How I can do to eliminate the lines that meet this condition? Help please. ... (4 Replies)
Discussion started by: </kida>
4 Replies

7. 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

8. 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

9. 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

10. Shell Programming and Scripting

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,... (3 Replies)
Discussion started by: toast
3 Replies
Login or Register to Ask a Question