Printing all combinations : Awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing all combinations : Awk
# 1  
Old 05-23-2008
Printing all combinations : Awk

Code:
$ 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:
Code:
AA1|BB1
AA1|BB2
AA1|BB3
AA2|BB1
AA2|BB2
AA2|BB3
AA3|BB1
AA3|BB2
AA3|BB3
AA4|BB1
AA4|BB2
AA4|BB3

I achieved this by writing this:
Code:
for KEY in `awk '/Start/ {print $1}' key.txt | tr '#' ' '`
        do
                sed -n "/$KEY Start/,/$KEY Complete/p" key.txt > .$KEY.tmp
        done

for id1 in `awk 'BEGIN {FS="|"} !/^#/ && NF!=0 {print $2}' .ID1.tmp`
        do
                for id2 in `awk 'BEGIN {FS="|"} !/^#/ && NF!=0 {print $2}' .ID2.tmp`
                        do
                                echo "$id1|$id2"
                        done
        done

Which worked for me :-)

Now problem is that, if in future somebody adds one more key to key.txt

i.e.
Code:
#ID1 Start
1|AA1
2|AA2
3|AA3
4|AA4
#ID1 Complete

#ID2 Start
1|BB1
2|BB2
3|BB3
#ID2 Complete

#ID3 Start
1|DD1
2|DD2
#ID3 Complete

I want to make my code generic, such that it takes all keys(ID1,ID2,ID3...) in key.txt and print the way I printed above.
(The hierarchy of KEY is as the order in key.txt)

i.e.

Code:
for loop for ID1
{
	for loop for ID2
		{
			for loop for ID3 
                           { ... }

		}

}

Please help.
# 2  
Old 05-23-2008
Not quite sure ...
Something like this?
(use nawk or /usr/xpg4/bin/awk on Solaris)

Code:
awk 'f && /^[0-9]/ { _[$1] = $2; c = $1; next }
!NF { f = 0 }
/^[0-9]/ { __[++n] = $2 }
END { for (i=1; i<=c; i++) {
  for(j=1; j<=n; j++)
    print _[i] FS __[j] }
}' FS=\| f=1 file

# 3  
Old 05-23-2008
Another approach:

Code:
awk 'BEGIN{FS=OFS="|"}
NR==FNR {
if(/#ID2 Start/){f=1;next}
if(/#ID2 Complete/){f=0}
if(f){a[i++]=$2}
next}
/#ID1 Start/{f=1;next}
/ID1 Complete/{exit}
{for(i in a){print $2,a[i]}}
'  file file

If you get errors use nawk or /usr/xpg4/bin/awk on Solaris.
# 4  
Old 05-26-2008
Thanks to radoulov and Franklin for your replies.

Just to make my requirement simple.

Code:
$ ls /temp
one  three  two

$ cat one
1
2
3

$ cat two
4
5

$ cat three
6
7
8

$ cat comb.sh
for i1 in `cat one`
        do
                for i2 in `cat two`
                        do
                                for i3 in `cat three`
                                        do
                                                echo "$i1|$i2|$i3"
                                        done
                        done
        done

$ ./comb.sh
1|4|6
1|4|7
1|4|8
1|5|6
1|5|7
1|5|8
2|4|6
2|4|7
2|4|8
2|5|6
2|5|7
2|5|8
3|4|6
3|4|7
3|4|8
3|5|6
3|5|7
3|5|8

But above code is hard-coded (I am specifying the file names in the code, I don't want to do that). what I want is that suppose if I add one more file called "four" with entries

Code:
$ cat four
9

Without adding a "for loop" for file "four", it should take automatically:

so that the output becomes:

Code:
1|4|6|9
1|4|7|9
1|4|8|9
1|5|6|9
1|5|7|9
1|5|8|9
2|4|6|9
2|4|7|9
2|4|8|9
2|5|6|9
2|5|7|9
2|5|8|9
3|4|6|9
3|4|7|9
3|4|8|9
3|5|6|9
3|5|7|9
3|5|8|9

i.e. How to construct the above "for loops" automatically based upon the files in /temp;; (Assuming the order of the loops is defined in a separate file)

Code:
$ cat order
one
two
three
four

Apologies for posting a huge query.
# 5  
Old 05-26-2008
Below code take a and b as the file name

Code:
sed '1d;$d' a | awk 'BEGIN{FS="|"}{print $2}'> a.t
sed '1d;$d' b | awk 'BEGIN{FS="|"}{print $2}'> b.t
while read aline
do
	while read bline
	do
		echo $aline"|"$bline
	done < b.t
done < a.t
rm a.t b.t

# 6  
Old 05-26-2008
Thank you Summer_Cherry for your reply. But its not printing the combinations of the values in the files as I stated.
# 7  
Old 05-28-2008
Try the following script :
Code:
$ cat comb.sh
awk '
{
   file_count++;
   rec_num = 0;
   while (getline rec <$0)
     records[file_count, ++rec_num] = rec;
   close($0);
   records[file_count, "count"] = rec_num;
}

function comb_file(file_num, rec     ,rec_count ,r) {
   if (file_num > file_count) {
      print rec;
   } else {
      rec_count = records[file_num, "count"];
      for (r=1; r<=rec_count; r++)
         comb_file(file_num+1, rec (file_num>1 ? "|" : "") records[file_num, r]);
   }
}

END {
   rec = "";
   comb_file(1, rec);
}

' file_order.txt
$

Input files:
Code:
$ cat file_order.txt
file_1.txt
file_2.txt
file_3.txt
$ cat file_1.txt
F1.1
F1.2
F1.3
$ cat file_2.txt
F2.1
F2.2
$ cat file_3.txt
F3.1
F3.2
F3.3
F3.4
$

Output :
Code:
$ comb.sh
F1.1|F2.1|F3.1
F1.1|F2.1|F3.2
F1.1|F2.1|F3.3
F1.1|F2.1|F3.4
F1.1|F2.2|F3.1
F1.1|F2.2|F3.2
F1.1|F2.2|F3.3
F1.1|F2.2|F3.4
F1.2|F2.1|F3.1
F1.2|F2.1|F3.2
F1.2|F2.1|F3.3
F1.2|F2.1|F3.4
F1.2|F2.2|F3.1
F1.2|F2.2|F3.2
F1.2|F2.2|F3.3
F1.2|F2.2|F3.4
F1.3|F2.1|F3.1
F1.3|F2.1|F3.2
F1.3|F2.1|F3.3
F1.3|F2.1|F3.4
F1.3|F2.2|F3.1
F1.3|F2.2|F3.2
F1.3|F2.2|F3.3
F1.3|F2.2|F3.4
$

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

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

I have a 1-column file such as the example below: 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: flower flower cat flower dog flower pizza ... (5 Replies)
Discussion started by: owwow14
5 Replies

5. Shell Programming and Scripting

Printing using awk

Hi I am relatively new to awk so i am getting confused a lot I am in need of help ... I am trying to append coloumns to the end of line using AWK I tried using this command awk -F "," '{for(s=7;s<=217;s++);$s="0";}1' OFS=, sam_sri_out It is giving me an output like this...... (1 Reply)
Discussion started by: Sri3001
1 Replies

6. 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

7. Shell Programming and Scripting

Awk printing help

Hallo, i have a file which looks like this: $1 $2 $3 Student1 55 Pass 55 Pass 35 Fail Student2 55 Pass 55 Pass 35 Fail i want that the $1 field... (3 Replies)
Discussion started by: saint2006
3 Replies

8. Shell Programming and Scripting

AWK printing

i have a file containing a line 123456 is it possible to use AWK to print it out to look like 1 2 3 4 5 6 (8 Replies)
Discussion started by: tomjones
8 Replies

9. Shell Programming and Scripting

AWK printing

Hello, I am trying to write a formatted report into a file using .ksh script and awk. Here is the command I am trying to run echo "before awk" ${SRC_SCHEMA} echo | awk '{printf "%-20s", ${SRC_SCHEMA} }' >>$REPORT_SQL_NAME I get the following error before awk ADW awk: 0602-562 Field $()... (1 Reply)
Discussion started by: fastgoon
1 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