How to create all possible combination of lines?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to create all possible combination of lines?
# 1  
Old 05-30-2008
How to create all possible combination of lines?

Hi,

I have a file where all the lines are sorted and uniq.
A part of it looks like

Code:
AAL
AAR
ABC
ABE
ABJ
ABQ
ABV
ABZ
ACC
ACE
ADA
ADB
ADD
ADL
AES

what I want to do is to create a new file which will contain all the possible combinations.

What I mean with simple abc example is

I have

a
b
c
d

and I want in my new file to have

a,b
a,c
a,d
b,a
b,c
b,d
c,a
c,b
c,d
d,a
d,b
d,c

I don't know how to do it.
Can somebody please advise?
# 2  
Old 05-30-2008
If it is only one level deep, try:

Code:
$ awk  
'{ a[$0] }
END {
        for (i in a){
                for (j in a){
                        if (i != j)  print (i "," j)
                }
        }
}' your-file


Last edited by ripat; 05-30-2008 at 03:03 PM.. Reason: correction not to print (a,a) (b,b) etc..
# 3  
Old 05-30-2008
here's a sample of how to 'permute'.
nawk -f permute.awk < /dev/null
OR
nawk -v str='dog cat cow' -f permute.awk < /dev/null

permute.awk:
Code:
BEGIN {
  str = ( str != "") ? str : "1 2 3"
  strN=split(str, arr, " ")
  permute(arr, 1, strN)
}

function printV(v, size,   i)
{
    for (i = 1; i <= size; i++) {
      printf("%5s", v[i] );
    }
    printf("\n");
}


function permute(v, start, n,    i,tmp)
{
  if (start == n) {
    printV(v, n);
  }
  else {
    for (i = start; i <= n; i++) {
      tmp = v[i];

      v[i] = v[start];
      v[start] = tmp;
      permute(v, start+1, n);
      v[start] = v[i];
      v[i] = tmp;
    }
  }
}

# 4  
Old 06-02-2008
Quote:
Originally Posted by ripat
If it is only one level deep, try:

Code:
$ awk  
'{ a[$0] }
END {
        for (i in a){
                for (j in a){
                        if (i != j)  print (i "," j)
                }
        }
}' your-file

Works great thanks!!!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Create 'n' number random pairwise combination of words

File 1 contains the list of words that needed to be randomly paired: Tiger Cat Fish Frog Dog Mouse Elephant Monkey File 2 contains the pairs that should not be used (in any solution) during random pairing. Elephant-Dog Cat-Fish Monkey-Frog Dog-Elephant, Fish-Cat, Frog-Monkey... (1 Reply)
Discussion started by: sammy777888
1 Replies

2. Shell Programming and Scripting

awk or a combination of commands to read and calculate nth lines from pattern

Two numerical lines, found by either header line, need to be added and the total placed in a new-header section. Also the total should should be rounded or cut to a two decimal anynumber.XX format with the AB string added on the end. For example: The numerical lines from headers 2 and 3 are... (3 Replies)
Discussion started by: jessandr
3 Replies

3. Shell Programming and Scripting

Read Lines from One file.. and create another.. and

Hello Members, I have one file which contains million of supplier code. I need to load these codes into database 1000 at a time. Database procedure reads from an external table which is based on the unix files. All I want to do is to read from the bigger file e.g. MAIN_FILE.txt and create... (1 Reply)
Discussion started by: chetanojha
1 Replies

4. Shell Programming and Scripting

How to create a file contains millions of lines and each line has different datas?

I want to create a file contains millions of lines. The line format like this: acnum$$123456$$+$$Tom$$111$$ fields separated by $$, field 1 and field 3 have only two options:acnum or crenum; + or -. field 4 can be any name or any letters. Other fields can be any fixed length digits. So, I want to... (9 Replies)
Discussion started by: hhdzhu
9 Replies

5. Shell Programming and Scripting

Please help, need to create script to remove lines by date in file

Please Help (novice to PERL and SHELL scripting)…. Need to create a script which removes all lines in $filename = "cycle_calendar_ftp_out" older than current date – a variable which will be a number of days passed to script. For Ex it will look at the end date which is the last field (4) and... (2 Replies)
Discussion started by: m3pwr
2 Replies

6. Shell Programming and Scripting

Ignoring lines and create new file

Hello, I have a requirement to ignore few lines in a file before keyword FILEHEADER . As soon as there is keyword FILEHEADER is identified in file , it will form another file with data from FILEHEADER to whatever in file after FILEHEADER. I wrote filename=$1 awk... (4 Replies)
Discussion started by: callmatkarna
4 Replies

7. Shell Programming and Scripting

Create new lines using a delimited string.

Hi I have a text file called 'fileA' which contains the follwoing line examples 01:rec1:25,50,75,100 02:rec2:30,60 03:rec3:20,40 I would like to create a new file where each of the comma separated values appears on a new line but prefixed with the first two fields e.g. 01:rec1:25... (3 Replies)
Discussion started by: mackmb
3 Replies

8. Shell Programming and Scripting

Wrap lines with awk to create SQL script

Greetings! Some of my files list hardware errors (we test electronic components), some have none. If the file name has no errors, I still want to display a message like "No error", else I display the error from the file itself. I came up with this (with help) for myfile in `find . -name... (2 Replies)
Discussion started by: alan
2 Replies

9. Shell Programming and Scripting

need to create lines from ones that begin with the field separator

Hello, I need to modify an awk script to recognize the last field $NF when the line is split over more than 1 line. In my input file the field separator is the exclamation mark ! so FS="!" So here is my input file infile.txt, it has 2 records, the field separator is in bold: INPUT ... (6 Replies)
Discussion started by: script_op2a
6 Replies

10. Shell Programming and Scripting

how to create one newfile and wants include some lines based on user request

I have one request, First it has to create one file and in that file it has to include some lines based on user request. Suppose user requested 10, then script needs to add 10 lines like below QAEVO_A1|A1 QAEVO_A2|A2 QAEVO_A3|A3 QAEVO_A4|A4 QAEVO_A5|A5 QAEVO_A6|A6 QAEVO_A7|A7... (8 Replies)
Discussion started by: sridhusha
8 Replies
Login or Register to Ask a Question