Finding a pattern with grep


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Finding a pattern with grep
# 1  
Old 03-11-2011
Java Finding a pattern with grep

I am interested in finding a pattern that looks like this

AABBACC

These letters are just for example but they can be any letter in the alphabet as long as they are different from each other, but the letter found in a certain spot should act like in the pattern above.

Thanks!

Last edited by moyzZ; 03-11-2011 at 06:45 AM.. Reason: thanks added ;)
# 2  
Old 03-11-2011
tested in bash:
Code:
grep -E '(.)\1(.)\2{1}\1(.)\3'

PS:this code cannot filter a sequence of the same leters, such as AAAAAAA.

Last edited by vistastar; 03-11-2011 at 09:58 PM..
# 3  
Old 03-11-2011
Quote:
Originally Posted by vistastar
tested in bash:
Code:
grep -E '(.)\1(.)\2{1}\1(.)\3'

Wonderful !! but could you please explain us the logic. Its very hard to crack Smilie
# 4  
Old 03-11-2011
Thanks, I'm looking for a DNA binding motif, so that some letters of the DNA code repeat in a certain order..
# 5  
Old 03-14-2011
Explanation

@royalibrahim

cat testfile

AABBACC

grep -E '(.)\1(.)\2{1}\1(.)\3' < testfile
AABBACC

What he did was, he used grep similar to egrep with the -E option, and then used grouping () and back referencing \

So, first he found the first character (with the wildcard period) and grouped it with the brackets, then back referenced it with the \1 - so now he has AA - then he found the next character (a B) and back referenced it with the \2 - so now he found AABB - the {1} means one time, then he looked for the first character again \1 and finally for the next character (second last - a "C") and he back referenced it, thus another C. All in all AABBACC.

This User Gave Thanks to sgruenwald For This Post:
# 6  
Old 03-14-2011
Quote:
Originally Posted by sgruenwald
@royalibrahim

cat testfile

AABBACC

grep -E '(.)\1(.)\2{1}\1(.)\3' < testfile
AABBACC

What he did was, he used grep similar to egrep with the -E option, and then used grouping () and back referencing \

So, first he found the first character (with the wildcard period) and grouped it with the brackets, then back referenced it with the \1 - so now he has AA - then he found the next character (a B) and back referenced it with the \2 - so now he found AABB - the {1} means one time, then he looked for the first character again \1 and finally for the next character (second last - a "C") and he back referenced it, thus another C. All in all AABBACC.

Would you shed some light on how to filter patern AAAAAAAA? thx!
# 7  
Old 03-15-2011
Reply to vistastar

Hi vistastar:

Then one has to do the following:

grep -E '(.)\1(.)\2\1(.)\3' testfile | grep -Ev '.(.)\1.{4}' | grep -Ev '...(.)\1.{2}' | grep -Ev '....(.)\1.' | grep -Ev '...(.).\1.'

the grep -Ev (extended negated option) tests whether second A is not first B '.(.)\1.{4}' and second B is not third A '...(.)\1.{2}' and third A is not first C '....(.)\1.' and second B is not first C '....(.)\1.' -- the other combinations are tested by the first expression. Here is a test of it:

Stefans-Computer:~ stefangr$ echo 'AABBACC' > testfile
Stefans-Computer:~ stefangr$ grep -E '(.)\1(.)\2\1(.)\3' testfile | grep -Ev '.(.)\1.{4}' | grep -Ev '...(.)\1.{2}' | grep -Ev '....(.)\1.' | grep -Ev '...(.).\1.'
AABBACC
Stefans-Computer:~ stefangr$ echo 'AAAACCC' > testfile
Stefans-Computer:~ stefangr$ grep -E '(.)\1(.)\2\1(.)\3' testfile | grep -Ev '.(.)\1.{4}' | grep -Ev '...(.)\1.{2}' | grep -Ev '....(.)\1.' | grep -Ev '...(.).\1.'
Stefans-Computer:~ stefangr$
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[sed] Finding and sticking the pattern to the beginning of successive lines up to the next pattern

I have a file like below. 2018.07.01, Sunday 09:27 some text 123456789 0 21 0.06 0.07 0.00 2018.07.02, Monday 09:31 some text 123456789 1 41 0.26 0.32 0.00 09:39 some text 456789012 1 0.07 0.09 0.09 09:45 some text 932469494 1 55 0.29 0.36 0.00 16:49 some text 123456789 0 48 0.12 0.15 0.00... (9 Replies)
Discussion started by: father_7
9 Replies

2. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

3. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

4. Shell Programming and Scripting

Finding the pattern and replacing the pattern inside the file

i have little challenge, help me out.i have a file where i have a value declared and and i have to replace the value when called. for example i have the value for abc and ccc. now i have to substitute the value of value abc and ccc in the place of them. Input File: go to &abc=ddd; if... (16 Replies)
Discussion started by: saaisiva
16 Replies

5. UNIX for Dummies Questions & Answers

Finding Pattern

Hi, i need some help regarding finding a pattern in files inside a direstory. i have these files abc123.txt cemj111.txt ckmem.txt cmick.txt crnnc.txt montt.txt xyz123.txt dfd123.txt cvv123.txt i need to find the files with name "*123.txt" which does not contain a perticular... (2 Replies)
Discussion started by: debu182
2 Replies

6. Homework & Coursework Questions

finding pattern without grep in unix

how can i find related pattern in a text file without using grep command in unix (2 Replies)
Discussion started by: feint
2 Replies

7. UNIX for Dummies Questions & Answers

Finding a pattern

Hi, I have the below content in file: <xmlfile> ows_Body="Hi" ows_Title="Title" ows_Author="krishna" </xmlfile> I wanted to remove ows_Body content from the file. I am using the below code sed -e 's/ows_Body.*ows/ows/g' Giving output: (9 Replies)
Discussion started by: mahish20
9 Replies

8. Shell Programming and Scripting

Want to grep for a pattern and display the content above that pattern

Hi, When we have a failure, sometimes we just step restart the job from the next step. Later when we open the log for analysis of the failure, it is becoming difficult to go to the failure part. For eg., if it is a 1000 line log, the failure may be at 500th line. so wat i want to do is, grep... (6 Replies)
Discussion started by: ajayakunuri
6 Replies

9. Shell Programming and Scripting

Finding Last occurance of another pattern when a pattern is found.

Hi, I have two files viz, rak1: $ cat rak1 rak2: $ cat rak2 sdiff rak1 rak2 returns: I want the lines that got modified, changed, or deleted preceding with the section they are in. I have done this so far: (1 Reply)
Discussion started by: rakeshou
1 Replies

10. Shell Programming and Scripting

finding duplicate files by size and finding pattern matching and its count

Hi, I have a challenging task,in which i have to find the duplicate files by its name and size,then i need to take anyone of the file.Then i need to open the file and find for more than one pattern and count of that pattern. Note:These are the samples of two files,but i can have more... (2 Replies)
Discussion started by: jerome Sukumar
2 Replies
Login or Register to Ask a Question