Delete Text between two strings sed, awk
2
0
Entering edit mode
6.3 years ago

I was searching for a solution, but could not find a proper one.

I want to remove all characters between two strings on every line.

Input is a Fasta-File:

>CAM_P0000101_READ_00457523 /accession=CAM_P0000101_READ_00457523 /xy=2625_3790 /region=2 /run=R_2008_08_11_16_51_31_ /length=253 /sample_id=1309720343513924875 /sample_acc=CAM_P0000101_SMPL_PAPUT2 /sample_name=CAM_P0000101_SMPL_PAPUT2 /site_id_n=CAM_P0000101_SITE_PAPUT2

GTGCCTTCGGGAACCGGGTGACAGGTGCTGCATGGCTGTCGTCAGCTCGTGTCGTGAGATGTTGGGTTAAGTCCCGTAACGAGCGCAACCCTTGTCCTTAGTTGCCAGCACGTAATGGTGGGAACTCTAAGGAGACTGCCGGTGACAAACCGGAGGAAGGTGGGGACGACGTCAAGTCATCATGGCCCTTACGGCCTGGGCTACACACGTGCTACAATGGCTAGGACAGACGGCTGCAAACCNGCGAGTGGGG

>CAM_P0000101_READ_00460168 /accession=CAM_P0000101_READ_00460168 /xy=2199_0493 /region=2 /run=R_2008_08_11_16_51_31_ /length=233 /sample_id=1309720343513924875 /sample_acc=CAM_P0000101_SMPL_PAPUT2 /sample_name=CAM_P0000101_SMPL_PAPUT2 /site_id_n=CAM_P0000101_SITE_PAPUT2

TTTACCGCGGCTGCTGGCACGAAGTTAGCCGGACCTTATTCTTCGGGTACAGTCATTATCTTTCCCGACAAAAGAGCTTTACAACCCAAGGGCCTTCTTCACTCACGCGGCATCGCTGCATCAGGCTTTCGCCCATTGTGCAAGATTCCCCACTGCTGCCTCCCGTAGGAGTCTGGGCCGTATCTCAGTCCCAGTGTGGCTGATCATCCTCTACAAATCAGCTATTGATTACT

I want to delete all text after first >CAM_P* to /sample_name=* and all after sample_name.*

>CAM_* /sample_name=* only these two things should remain.

all this should be removed :

/accession=CAM_P0000101_READ_00457523 /xy=2625_3790 /region=2 /run=R_2008_08_11_16_51_31_ /length=253 /sample_id=1309720343513924875 /sample_acc=CAM_P0000101_SMPL_PAPUT /site_id_n=CAM_P0000101_SITE_PAPUT2

Could anyone please help me ?

awk sed substitution delete bash • 4.3k views
ADD COMMENT
0
Entering edit mode

Thank you guys, you made my day !!!

ADD REPLY
3
Entering edit mode
6.3 years ago
Vincent Laufer ★ 1.4k

Definitely not the most elegant solution, but this command

awk '{gsub(/\/sample_name/,"\n>sample_name")}{gsub(/\//,"\n\/")}{print $0}' FASTA.txt | grep -v "/"

returns

>CAM_P0000101_READ_00457523
>sample_name=CAM_P0000101_SMPL_PAPUT2
GTGCCTTCGGGAACCGGGTGACAGGTGCTGCATGGCTGTCGTCAGCTCGTGTCGTGAGATGTTGGGTTAAGTCCCGTAACGAGCGCAACCCTTGTCCTTAGTTGCCAGCACGTAATGGTGGGAACTCTAAGGAGACTGCCGGTGACAAACCGGAGGAAGGTGGGGACGACGTCAAGTCATCATGGCCCTTACGGCCTGGGCTACACACGTGCTACAATGGCTAGGACAGACGGCTGCAAACCNGCGAGTGGGG

>CAM_P0000101_READ_00460168
>sample_name=CAM_P0000101_SMPL_PAPUT2
TTTACCGCGGCTGCTGGCACGAAGTTAGCCGGACCTTATTCTTCGGGTACAGTCATTATCTTTCCCGACAAAAGAGCTTTACAACCCAAGGGCCTTCTTCACTCACGCGGCATCGCTGCATCAGGCTTTCGCCCATTGTGCAAGATTCCCCACTGCTGCCTCCCGTAGGAGTCTGGGCCGTATCTCAGTCCCAGTGTGGCTGATCATCCTCTACAAATCAGCTATTGATTACT
ADD COMMENT
2
Entering edit mode
6.3 years ago
5heikki 10k

If all the headers are the same format (order of fields), then perhaps:

cut -f1,9 -d " " your.fasta > output.fasta

Afterwards you can check if grep -c '^>' and grep -c "sample_name" return the same number.

ADD COMMENT

Login before adding your answer.

Traffic: 2594 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6