Filter duplicate block of text using SED


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Filter duplicate block of text using SED
# 1  
Old 08-07-2008
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 test.txt contains following data.
**********************************************
start
test for the block
test for the block
test for the block
End
Blah Blah
Blah Blah
Blah Blah
start
test for the block
test for the block
test for the block
end

*******************
Now if i use sed command to print the text between regular expressions
"start" and "end"

sed -n '/start/,/end/p' text.txt >> ouput.txt

I get the block of text twice in output.txt file as shown below
********************************************************************
start
test for the block
test for the block
test for the block
end
start
test for the block
test for the block
test for the block
end
****************

Please help on how do I filter duplicate printing.

Thanks in Advance
Deepak
# 2  
Old 08-07-2008
Make sed quit when it encounters the end.

Code:
sed -n '/start/,/end/p;/end/q' text.txt >> output.txt

# 3  
Old 08-07-2008
Hi,

Thanks for your help and time on this, It works great.

Deepak.
# 4  
Old 10-22-2008
I'd like to piggy-back onto this post and ask, how do I read everything in between "start" and "end" so that "start" and "end" are not included in the extraction?

I recognize that I can use different words, but I want to keep "start" and "end" in my file. The reason is that I created a help page for a script I wrote. When option -h is used, it grabs the text from the file. It would be handy to have all my help pages begin with "start" and end with "end". But I don't want those words to display on the screen.

Thanks.
# 5  
Old 10-23-2008
Code:
sed -n '/start/,/end/p
 /end/q
 ' filename | sed '/^start$/d
/^end$/d'

# 6  
Old 10-23-2008
Or another idea:

Code:
awk '/^start$/ { while (getline && $0 !~ /^end$/) print }' inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

CSV File:Filter duplicate records from column1 & another column having unique record

Hi Experts, I have csv file with 30, 40 columns Pasting just 2 column for problem description. Need to print error if below combination is not present in file check for column-1 (DocumentNumber) and filter columns where value in DocumentNumber field is same. For all such rows, the field... (7 Replies)
Discussion started by: as7951
7 Replies

2. Shell Programming and Scripting

Filter duplicate records from csv file with condition on one column

I have csv file with 30, 40 columns Pasting just three column for problem description I want to filter record if column 1 matches CN or DN then, check for values in column 2 if column contain 1235, 1235 then in column 3 values must be sequence of 2345, 2345 and if column 2 contains 6789, 6789... (5 Replies)
Discussion started by: as7951
5 Replies

3. Shell Programming and Scripting

Filter file to remove duplicate values in first column

Hello, I have a script that is generating a tab delimited output file. num Name PCA_A1 PCA_A2 PCA_A3 0 compound_00 -3.5054 -1.1207 -2.4372 1 compound_01 -2.2641 0.4287 -1.6120 3 compound_03 -1.3053 1.8495 ... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

4. UNIX for Dummies Questions & Answers

Filter records in a huge text file from a filter text file

Hi Folks, I have a text file with lots of rows with duplicates in the first column, i want to filter out records based on filter columns in a different filter text file. bash scripting is what i need. Data.txt Name OrderID Quantity Sam 123 300 Jay 342 498 Kev 78 2500 Sam 420 50 Vic 10... (3 Replies)
Discussion started by: tech_frk
3 Replies

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

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

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

8. Shell Programming and Scripting

Filter or remove duplicate block of text without distinguishing marks or fields

Hello, Although I have found similar questions, I could not find advice that could help with our problem. The issue: We have several hundreds text files containing repeated blocks of text (I guess back at the time they were prepared like that to optmize printing). The block of texts... (13 Replies)
Discussion started by: samask
13 Replies

9. Shell Programming and Scripting

Filter/remove duplicate .dat file with certain criteria

I am a beginner in Unix. Though have been asked to write a script to filter(remove duplicates) data from a .dat file. File is very huge containig billions of records. contents of file looks like 30002157,40342424,OTC,mart_rec,100, ,0 30002157,40343369,OTC,mart_rec,95, ,0... (6 Replies)
Discussion started by: mukeshguliao
6 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