1

I am trying to search for a grep command that will allow me to extract two patterns (words) for every line with random sequence in the middle of a specific length (6 in my case):

ATCAACGCAGAGTACATXXXXXXGGG

Here in my case I have the two words: "ATCAACGCAGAGTACAT" and "GGG" with in the middle 6 letters "XXXXXX" but these can be whatever letter.

I have looked at the already available solution but there is none specific to this one I believe.

Input

ACGCAACGACGACG**ATCAACGCAGAGTACAT****XXXXXX****GGG**AATTTA
AGGGAACGTTGCCG**ATCAACGCAGAGTACAT****XXXXXX****GGG**CATGGA
GGAACGTTGCCAAG**ATCAACGCAGAGTACAT****XXXXXXXXX****GGG**ATG  (this does not match for example)  
AGGGAACGTTGCCG**ATCAACGCAGAGTACAT****XXXXXX****GGG**CATGGA

Output

ACGCAACGACGACG**ATCAACGCAGAGTACAT****XXXXXX****GGG**AATTTA
AGGGAACGTTGCCG**ATCAACGCAGAGTACAT****XXXXXX****GGG**CATGGA
AGGGAACGTTGCCG**ATCAACGCAGAGTACAT****XXXXXX****GGG**CATGGA

where "XXXXXX" can be every combination of A,C,T,G like AATCGA or AAATCC etc..

How could I do this with grep or sed?

8
  • 1
    Are the strings always the same length? Feb 21, 2020 at 13:34
  • ATCAACGCAGAGTACAT and GGG yes and also XXXXXX yes Feb 21, 2020 at 13:35
  • The Xs can be any letter? Like CAACGC? Or CAXXGC? Or should that be none of A,C,G,T? Or six consecutive identical letters like GGGGGG? Feb 21, 2020 at 13:37
  • can be A or C or G or T in what ever order. Other letters that are none them can be excluded (am working with DNA) Feb 21, 2020 at 13:38
  • 1
    What do the STARS mean?? Any sequence, then magic 6, then some more, then GGG?
    – user373503
    Feb 21, 2020 at 13:56

1 Answer 1

3

Try

grep "ATCAACGCAGAGTACAT[ACGT]\{6\}GGG" file
7
  • I think . would be adequate vs [ACGT] since ACGT seem to be the only characters in the OPs data (assuming the *s are the OPs attempt to highlight parts of the text, not actually part of the input).
    – Ed Morton
    Feb 21, 2020 at 13:51
  • 1
    Or grep -E 'ATCAACGCAGAGTACAT[XATGC]{6}GGG' dna for testing, and another regex type.
    – user373503
    Feb 21, 2020 at 13:52
  • I have already tried . following a previous post but does not work Feb 21, 2020 at 13:52
  • 3
    Naked dot with -E: grep -E 'ATCAACGCAGAGTACAT.{6}GGG' dna
    – user373503
    Feb 21, 2020 at 13:54
  • 1
    @fusion.slope .* means any number of characters, I'm suggesting .{6} which means 6 characters. Try exactly the command in @rastafile's comment. Not sure what you had in mind with ${...} - that'd be for using shell variables, not literal strings.
    – Ed Morton
    Feb 21, 2020 at 14:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .