Combine all lines of a file with all lines of another file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combine all lines of a file with all lines of another file.
# 1  
Old 12-01-2011
Combine all lines of a file with all lines of another file.

Dear all,
I would like to combine all lines of a file with all lines of another file:
The input are
file 1
A
B
C
D

file 2
A
B
C
D

The output is
final file
A_A
A_B
A_C
A_D
B_B
B_C
B_D
C_C
C_D
D_D

In the output file it was combined A_B but not B_A, because it is redundance for me and just one combination is enough. The same idea need to be applied for all lines.

NOTE: There are 39,278 lines in the file 1 and 39,278 lines in the file 2.The sequence of letters are equal in both files, such as in the example. I'm using OSX and the computer has 12 Gb of Ram.

How can I combine these lines?

Thanks in advance.
# 2  
Old 12-01-2011
Code:
#!/usr/bin/ksh
typeset -i mPos=1
while read m1; do
  sed -n "${mPos},\$s/^/${m1}_/p" One_File
  mPos=${mPos}+1
done < One_File

# 3  
Old 12-02-2011
perl

Code:
open $fh,"<a";
my @arr = <$fh>;
close $fh;
open $fh,"<b";
my @brr = <$fh>;
close $fh;
@arr=map {chomp;$_}@arr;
@brr=map {chomp;$_}@brr;
foreach my $a(@arr){
  foreach my $b(@brr){
    my @tmp = ($a,$b);
    my $str = join "_", sort @tmp;
    if(not exists($hash{$str})){
     print $str,"\n";
     $hash{$str}=1;
    }
  }
}


Last edited by Franklin52; 12-16-2011 at 11:52 AM.. Reason: Code tags
# 4  
Old 12-15-2011
Combine all lines of a file with all lines of another file.

Thanks. It works as I wanted.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to remove lines that do not start with digit and combine line or lines

I have been searching and trying to come up with an awk that will perform the following on a converted text file (original is a pdf). 1. Since the first two lines are (begin with) text they are removed 2. if $1 is a number then all text is merged (combined) into one line until the next... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Script to combine lines in a file

Greetings, I have large file with following format name1 name2 name3 name4 name5 name6 child7 child8 child9 <== there is leading blank space child10 child11 child12 <== there is leading blank space name13 name14 name15 name16 child17 child18... (6 Replies)
Discussion started by: rnnyusa
6 Replies

3. Shell Programming and Scripting

awk to combine matching lines in file

I am trying to combine all matching lines in the tab-delimited using awk. The below runs but no output results. Thank you :). input chrX 110925349 110925532 ALG13 chrX 110925349 110925532 ALG13 chrX 110925349 110925532 ALG13 chrX 47433390 47433999 SYN1... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

Program to combine two lines in a file on checking the first character of each line

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi, female,... (12 Replies)
Discussion started by: jayaP
12 Replies

5. UNIX for Advanced & Expert Users

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

6. UNIX for Dummies Questions & Answers

Need to combine two lines in a file based on first character of each line in a file

Hi, I have a requirement where I need to combine two lines in a file based on first character of each line in a file. Please find the sample content of the file below: Code: _______________________ 5, jaya, male, 4-5-90, single smart 6, prakash, male, 5-4-84, married fair 7, raghavi,... (1 Reply)
Discussion started by: jayaP
1 Replies

7. UNIX for Dummies Questions & Answers

Combine lines in file

Hi All, I am trying to understand if its possible to carry out the following. I have a text file which contains output from multiple commands, within the file a node will be quiered twice if there was 2 commands for example. Is it possible do combine 2 lines into 1 if the first word is the... (1 Reply)
Discussion started by: mutley2202
1 Replies

8. UNIX for Dummies Questions & Answers

Combine lines from file

Hello, Input file looks like this: apples bananas oranges and rice pears cherries mango I want output to look like this: apples bananas oranges and rice pears cherries mango It should combine line 1 with line 2 and line 3 with line 4 like that.... Right now, only way I can... (4 Replies)
Discussion started by: holyearth
4 Replies

9. Shell Programming and Scripting

Combine multiple lines in file based on specific field

Hi, I have an issue to combine multiple lines of a file. I have records as below. Fields are delimited by TAB. Each lines are ending with a new line char (\n) Input -------- ABC 123456 abcde 987 890456 7890 xyz ght gtuv ABC 5tyin 1234 789 ghty kuio ABC ghty jind 1234 678 ght ... (8 Replies)
Discussion started by: ratheesh2011
8 Replies

10. UNIX for Dummies Questions & Answers

How do I combine the last 2 lines of a file

I tried to put the history line number and the date into the file with one command, and failed. Can't figure out how to get the date variable substituted for the last space captured. history | tail -1 | sed -e 's/.\{7\}/&/g' | head -1 | sed 's/ $/$date/' Result was: 729 $date So, I... (8 Replies)
Discussion started by: jimbob75
8 Replies
Login or Register to Ask a Question