LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-26-2011, 05:42 PM   #1
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 122

Rep: Reputation: 15
Need help with sed command: if a line contains >2 colons (:) delete it and line above


Hi all,

I'm trying to put together a simple sed script but I need some help with a regexp. I want to delete/exclude each line that contains >2 colon characters (but containing exactly 2 colons is OK) AND delete the line directly above lines that contain >2 colons. The lines containing colons are full of other ASCII characters.

Example input file:
Code:
>E52LN6201CQPC2 length=145 xy=1007_0596 region=1 run=R_2008_02_25_06_58_44_tetranucleotide 29 : 128 -- length 100 score 61 unit AGAT
TTCCTACCTACCTATCCATCTATCTCTC:CTATCTATCTATCTATCCATCTATCTATCTATCTATCTATCTATCTATCCATCTATCTATCTATCTGTCTATCTATCCATCTATCTACCTATCTATCTAT:TTTTCTCCTTCTCTTCT
>E52LN6201CJ7P2 length=134 xy=0933_0904 region=1 run=R_2008_02_25_06_58_44_tetranucleotide 29 : 128 -- length 100 score 61 unit AGAT
TTCCTACCTACCTATCCATCTATCTCTC:CTATCTATCTATCTATCCATCTATCTATCTATCTATCTATCTATCTATCCATCTATCTATCTATCTGTCTATCTATCCATCTATCTACCTATCTATCTAT:TTTTTC
>E52LN6201BHNP5 length=220 xy=0494_0203 region=1 run=R_2008_02_25_06_58_44_tetranucleotide 81 : 220 -- length 140 score 136 unit unk
AAT:TAAAAAGATTTCTAAAATGTGTAT:ATACATAAAGGTAGGCAGTGTGTGAAAG:AAAGAAAGAATGAGATGGTAGAGAA:AGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAGAG
In the example above, the last sequence contains 4 colons so I would want it deleted. The desired output would be the following:
Code:
>E52LN6201CQPC2 length=145 xy=1007_0596 region=1 run=R_2008_02_25_06_58_44_tetranucleotide 29 : 128 -- length 100 score 61 unit AGAT
TTCCTACCTACCTATCCATCTATCTCTC:CTATCTATCTATCTATCCATCTATCTATCTATCTATCTATCTATCTATCCATCTATCTATCTATCTGTCTATCTATCCATCTATCTACCTATCTATCTAT:TTTTCTCCTTCTCTTCT
>E52LN6201CJ7P2 length=134 xy=0933_0904 region=1 run=R_2008_02_25_06_58_44_tetranucleotide 29 : 128 -- length 100 score 61 unit AGAT
TTCCTACCTACCTATCCATCTATCTCTC:CTATCTATCTATCTATCCATCTATCTATCTATCTATCTATCTATCTATCCATCTATCTATCTATCTGTCTATCTATCCATCTATCTACCTATCTATCTAT:TTTTTC
Here's what I have so far for the sed command (where out.txt would be the file I want to keep):
Code:
sed -n '/????/{n;x;d;};x;1d;p; ${x;p}' in.txt > out.txt
The part in bold (????) is the part that I don't know how to specify. Any help / suggested reading would be greatly appreciated!

Thanks,
Kevin
 
Old 12-27-2011, 09:51 AM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,608

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
Hi,

try this:
Code:
sed -r '/^>/{h;d};/([^:]+:){3}/d;x;p;x'
Awk is also interesting for this kind of task:
Code:
awk -F ":" '{if (/^>/){a=$0;next;}if (NF<4){print a;print}}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 11:48 AM
Search word and delete only the word and the line using Sed command kbmukesh Linux - Newbie 4 06-28-2011 07:35 AM
Delete next line in sed Yalla-One Programming 9 01-04-2009 05:00 PM
sed to delete a line for a word and line above cmontr Programming 11 07-03-2008 09:33 AM
SED - Delete line above or below as well as matching line... OldGaf Programming 7 06-27-2008 12:51 AM


All times are GMT -5. The time now is 10:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration