Print combinations of alphabets in a sequence


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print combinations of alphabets in a sequence
# 1  
Old 04-02-2013
Print combinations of alphabets in a sequence

Hi Friends,

I have a series of alphabets like this

Code:
[CA]A[GCAT][ATG][AA]GCAA[TG]

The values inside the square brace indicate that either of them can be present at that position. And those ones without a brace, means that they are the only ones that could be printed at that location.

Now, I would like to know if there is a possibility for a script to print all possible combinations of these alphabet occurrences.

For an example sake, if I consider only the first alphabet within a square brace, the output would be

Code:
CAGAAGCAAT

The alphabets in bold indicate the first ones in the square brace. Like this way, is there a possibility to print all possible combinations?

Thanks
# 2  
Old 04-02-2013
Shell has property in which it expands anything given in braces:

Code:
$ echo AB{c,d}E
ABcE ABdE


So, its just a matter of replacing the square brackets with curly braces and putting comma between characters, and using the shell expansion property:

Code:
$ x='[CA]A[GCAT][ATG][AA]GCAA[TG]'
$ eval echo $(echo $x | perl -pe 'tr/][/}{/;s/\{([A-Z]+)\}/"{" . join (",",split("",$1)) . "}"/eg;')

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 3  
Old 04-02-2013
Quote:
Originally Posted by guruprasadpr
Shell has property in which it expands anything given in braces:

Code:
$ echo AB{c,d}E
ABcE ABdE


So, its just a matter of replacing the square brackets with curly braces and putting comma between characters, and using the shell expansion property:

Code:
$ x='[CA]A[GCAT][ATG][AA]GCAA[TG]'
$ eval echo $(echo $x | perl -pe 'tr/][/}{/;s/\{([A-Z]+)\}/"{" . join (",",split("",$1)) . "}"/eg;')

Guru.
That was a great solution. Thanks guru! Instead of printing them side by side, is there a way to print one under the other.

Thanks once again for your time.
# 4  
Old 04-02-2013
Brilliant solution indeed, guruprasadpr!

@jacobs.smith: try:
Code:
$ echo -e {C,A}A{G,C,A,T}{A,T,G}GCAA{T,G}"\n"

or
Code:
$ printf "%s\n" {C,A}A{G,C,A,T}{A,T,G}GCAA{T,G}

# 5  
Old 04-03-2013
thanks for your comments.

@jacobs:
Modifying using printf as RudiC has used:
Code:
$ printf "%s\n" $(eval echo $(echo $x | perl -pe 's/\[([A-Z]+)\]/"{" . join (",",split("",$1)) . "}"/eg;'))

Also, tweaked the solution a little by removing the tr part.

Guru.
# 6  
Old 04-03-2013
A variation on a theme which eliminates eval, echo, and the command substitution subshell:
Code:
perl -pe 's/.../.../eg; $_ = "printf %s\\\\n " . $_;' | sh

Obviously, sh should be a shell which supports brace expansion.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Print a python script down a list in a text file without printing a lot combinations

In a python script I have 2 files printing side by side on the same line. I want to have 1 of the files to be already displayed at once while the other file print down the list in the file and it still will produce new lines. I want to do it like that to reduce printing a lot of lines and... (0 Replies)
Discussion started by: bigvito19
0 Replies

2. UNIX for Beginners Questions & Answers

Print Line as per the dependent sequence in shell script.

Hi i have a file like this as shown below: DA PROCESS_ID IDENTIFIER DA_FILE STATUS WAITING_FOR SCOPED_DEPENDENT 1836 21000 01052019 BH90P.TEMP.DA1836.FTP W NULL ... (6 Replies)
Discussion started by: krishnaswarnkar
6 Replies

3. Homework & Coursework Questions

program to find and print a Fibonacci sequence of numbers. --Errors

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I am trying to convert a C language program over to Sparc Assembley and I am getting Undefined first referenced... (4 Replies)
Discussion started by: kenjiro310
4 Replies

4. Shell Programming and Scripting

find common entries and match the number with long sequence and cut that sequence in output

Hi all, I have a file like this ID 3BP5L_HUMAN Reviewed; 393 AA. AC Q7L8J4; Q96FI5; Q9BQH8; Q9C0E3; DT 05-FEB-2008, integrated into UniProtKB/Swiss-Prot. DT 05-JUL-2004, sequence version 1. DT 05-SEP-2012, entry version 71. FT COILED 59 140 ... (1 Reply)
Discussion started by: manigrover
1 Replies

5. Shell Programming and Scripting

Perl : print the sequence number without missing number

Dear Perl users, I need your help to solve my problem below. I want to print the sequence number without missing number within the range. E.g. my sequence number : 1 2 3 4 5 6 7 8 11 12 13 14 my desired output: 1 -8 , 11-14 my code below but still problem with the result: 1 - 14 1 -... (2 Replies)
Discussion started by: mandai
2 Replies

6. Shell Programming and Scripting

print out missing files in a sequence

Hello all, I have several directories with a sequence of files like this IM-0001-0001.dcm IM-0001-0002.dcm IM-0001-0003.dcm IM-0001-0004.dcm IM-0001-0005.dcm I would like to print out the name of the file that is missing. I currently have the following ineffecient way to do this... (4 Replies)
Discussion started by: avatar_007
4 Replies

7. Shell Programming and Scripting

awk -- print combinations for 2 cols

Dear all, could you please help me with awk please? I have such input: Input: a d b e c f The number of lines is unknown before reading the file. I need to print possible combination between the two columns like this: Output: a d b d c d a e b e c e a f (2 Replies)
Discussion started by: irrevocabile
2 Replies

8. Shell Programming and Scripting

count alphabets in a flat file and print

I have a text file with a huge dataset, and each row in that dataset has some data(65479 rows). In that file I need to find the number of times a-z & A-Z Appears. How Can I Initialise Array into an Array to parse the count also I need to parse a,A into a single array preferably. example of... (3 Replies)
Discussion started by: vmsenthil
3 Replies

9. Shell Programming and Scripting

Checking for Alphabets

echo -n "read this also:" read NewAuthor if ]' ) ] ; then echo "its a digit" else echo "something else" fi Hey guys , i am trying to do a search to check if the input is using alphabets and nothing else. I tried using ] and ] but none seems to work When i use digit, it read 22.k... (5 Replies)
Discussion started by: gregarion
5 Replies

10. UNIX for Advanced & Expert Users

Extracting only Alphabets from a value

Hi, I have file name (abcd001). I want to extract on the alphabets from this file name. I don't want the numeric part of it. Once i extract the alphabets the i can search for all those file. Could anyone help on this. Thanks in advance (2 Replies)
Discussion started by: amitkhiare
2 Replies
Login or Register to Ask a Question