Creating all possible bi-combinations of a list with grep/awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Creating all possible bi-combinations of a list with grep/awk
# 1  
Old 05-07-2014
Creating all possible bi-combinations of a list with grep/awk

I have a 1-column file such as the example below:

Code:
flower
cat
dog
pizza
car
book

I want to create a second column in which all possible 2-item combinations of column one are outputted in 2 tab-separated columns, such as:

Code:
flower    flower
cat    flower
dog     flower
pizza     flower
car     flower
book     flower
flower     cat
cat     cat
dog     cat
pizza     cat
car     cat
book     cat
flower    dog
cat    dog
dog    dog
pizza    dog
car    dog
book    dog
flower    pizza
cat    pizza
dog    pizza
pizza    pizza
car    pizza
book    pizza
flower    car
cat    car
dog    car
pizza    car
car    car
book    car
flower    book
cat    book
dog    book
pizza    book
car    book
book    book

Is there a
Code:
grep / awk

that allows me to do this?
Thanks
# 2  
Old 05-07-2014
Code:
awk '
        {
                A[++c] = $1
        }
        END {
                for ( i = 1; i <= c; i++ )
                {
                        for ( j = 1; j <= c; j++ )
                        {
                                print A[j], A[i]
                        }
                }
        }
' file

These 2 Users Gave Thanks to Yoda For This Post:
# 3  
Old 05-07-2014
Hi Yoda,

If possible could you please explain your code line by line.
What this A[++c] will stores .. like this...

Regards,
This User Gave Thanks to Makarand Dodmis For This Post:
# 4  
Old 05-07-2014
Code:
while read REC1
do
   while read REC2
   do
      echo $REC2"               "$REC1
   done<0002.file
done<0002.file
#
                     
flower          flower
cat             flower
dog             flower
pizza           flower
car             flower
book            flower
flower          cat
cat             cat
dog             cat
pizza           cat
car             cat
book            cat
flower          dog
cat             dog
dog             dog
pizza           dog
car             dog
book            dog
flower          pizza
cat             pizza
dog             pizza
pizza           pizza
car             pizza
book            pizza
flower          car
cat             car
dog             car
pizza           car
car             car
book            car
flower          book
cat             book
dog             book
pizza           book
car             book
book            book

what do you grep to be used for???
# 5  
Old 05-07-2014
++c means increment variable c value and it is used as array index to store each record in the file A[++c] = $1

In the END rule, two for loops are used to print each array value for different indexes.
# 6  
Old 05-07-2014
Code:
join -12 -22 -o 1.1,2.1 -t '
' file file | sed 'N; s/\n/ /'

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. Shell Programming and Scripting

awk permutations and combinations

hello, I'm reading this thread, in which there is this code :awk ' function comb(v,i) { for(i in A) { delete A; if(length(A)) comb((v?v"+":x)i) else print v"+"i A; } } { A } END { comb(); } ' infilebut I can't understand where does v come... (5 Replies)
Discussion started by: daPeach
5 Replies

3. UNIX for Dummies Questions & Answers

Generating all possible combinations of values in field 1 (awk)

Input: A|1 B|2 C|3 D|4 Output: A+B|3 A+C|4 A+D|5 B+C|5 B+D|6 C+D|7 A+B+C|6 A+B+D|7 A+C+D|8 B+C+D|9 A+B+C+D|10 I only managed to get the output for pairs of $1 values (i.e. combination of length 2): (4 Replies)
Discussion started by: beca123456
4 Replies

4. OS X (Apple)

Having trouble creating an alias for grep

Hi, I'm using Mac 10.9.1. I would like to create an alias for grep so that it won't print out messages like "grep: /Users/davea/workspace/myproject/subdir/: Is a directory" all the time. So in my terminal, I opened ~/.profile and entered alias grep='grep -s' However, when I close and... (5 Replies)
Discussion started by: laredotornado
5 Replies

5. UNIX for Dummies Questions & Answers

Creating a two column list of date pairs form a single column list

Hi all, looking for some help here. I'm what you'd call a dirty programmer. my shell scripts might be ugly, but they (usually) function... Say I have a single column text file with a list of dates (yyyymmdd) that represent the elevation of a point on that date (I work with land subsidence, so... (2 Replies)
Discussion started by: jbrandt1979
2 Replies

6. UNIX for Dummies Questions & Answers

Creating a column based list from a string list

I have a string containing fields separated by space Example set sr="Fred Ted Joe Peter Paul Jean Chris Tim Tex" and want to display it in a column format, for example to a maximum of a window of 100 characters And hopefully display some thing like Fred Ted Joe ... (3 Replies)
Discussion started by: kristinu
3 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. UNIX for Dummies Questions & Answers

Creating a one liner with wget, grep and mv

I have the following simplified script, that consists of a download (wget), a grab of only the lines with a comma (grep) and a rename of the file to indicate its date of origin (mv): wget http://www.example.com/datafile.csv grep ',' datafile.csv mv datafile.csv datafile.`date... (2 Replies)
Discussion started by: figaro
2 Replies

9. Shell Programming and Scripting

Printing all combinations : Awk

$ cat key.txt #ID1 Start 1|AA1 2|AA2 3|AA3 4|AA4 #ID1 Complete #ID2 Start 1|BB1 2|BB2 3|BB3 #ID2 Complete I was required this output: AA1|BB1 AA1|BB2 AA1|BB3 (7 Replies)
Discussion started by: jkl_jkl
7 Replies

10. Shell Programming and Scripting

Awk execution with more combinations..

The requirement is to extract 9 fields randomly from a 13 field CSV file to make another CSV file. While doing that, the field 7 and 8 of input csv file should comeout as field 7 of output with the value as first charecters of both 7 and 8 fields of input file(CSV file). Presently I am using... (1 Reply)
Discussion started by: mitte_dino
1 Replies
Login or Register to Ask a Question