Combine Two Text Files (PERMUTE)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combine Two Text Files (PERMUTE)
# 1  
Old 12-15-2015
Display Combine Two Text Files (PERMUTE)

Hello everybody,

I would like to know how can I obtain this:

There are two text files.....ffirst.txt and fsecond.txt

ffirst.txt contains 5 lines (example):
Code:
A
B
C
D
E

fsecond.txt contains 10 lines (example):
Code:
1
2
3
4
5
6
7
8
9
10

NOW, I would like know how to create this from this 2 text files via linux shell script:
THE OUTPUT:

Code:
A1
A2
A3
A4
A5
B1
B2
B3
B4
B5
C1
C2
C3
C4
C5
D1
D2
D3
D4
D5
E1
E2
E3
E4
E5

If there is a solution on this forum already (I've searched it), please excuse me, and post me an redirection link. Cheers!

Last edited by Don Cragun; 12-15-2015 at 04:04 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 12-15-2015
Please use code tags as required by forum rules!

Any attempts/ideas/thoughts from your side?

---------- Post updated at 21:06 ---------- Previous update was at 21:04 ----------

Why do you limit the fsecond.txt file's lines to 5?

---------- Post updated at 21:08 ---------- Previous update was at 21:06 ----------

Howsoever, without the limit, this might work:
Code:
while read A; do while read B; do echo $A$B; done <file4; done < file3
A1
A2
A3
A4
A5
A6
A7
A8
A9
A10
B1
B2
B3
B4
B5
.
.
.

This User Gave Thanks to RudiC For This Post:
# 3  
Old 12-15-2015
Quote:
Originally Posted by RudiC
Please use code tags as required by forum rules!

Any attempts/ideas/thoughts from your side?

---------- Post updated at 21:06 ---------- Previous update was at 21:04 ----------

Why do you limit the fsecond.txt file's lines to 5?

---------- Post updated at 21:08 ---------- Previous update was at 21:06 ----------

Howsoever, without the limit, this might work:
Code:
while read A; do while read B; do echo $A$B; done <file4; done < file3
A1
A2
A3
A4
A5
A6
A7
A8
A9
A10
B1
B2
B3
B4
B5
.
.
.

No, sir, I have put (example) as a mark that this 10-line length is just an example.

What am I about to use is a dictionary text file with about 5000 lines.
The essence here is...that those two files does not have same number of lines AND the script should end when the last line from
Code:
ffirst.txt

is combined with all the lines from
Code:
fsecond.txt

.

I am about to try your suggested solution, and see how it goes.

Anyways, thank you for such a prompt response!

Best regards, gandrinno!
# 4  
Old 12-15-2015
Quote:
Originally Posted by gandrinno1
.
.
.
The essence here is...that those two files does not have same number of lines AND the script should end when the last line from
Code:
ffirst.txt

is combined with all the lines from
Code:
fsecond.txt

.

.
.
.
Then your sample output is misleading, and the code given should work as desired.
However, with one small and one large file, that code is suboptimal. It made more sense to read the small file's values into an array and then loop through the array for every large file's line...

---------- Post updated at 21:59 ---------- Previous update was at 21:53 ----------

Got a recent bash? Then try
Code:
readarray -t A <file3
while read B; do for IX in ${!A[@]}; do echo ${A[$IX]}$B; done; done < file4
A1
B1
C1
D1
E1
A2
B2
C2
D2
E2
A3
B3
C3
D3
E3
A4
B4
C4
D4
E4
A5
B5
.
.
.

This User Gave Thanks to RudiC For This Post:
# 5  
Old 12-15-2015
Quote:
Originally Posted by RudiC
Then your sample output is misleading, and the code given should work as desired.
However, with one small and one large file, that code is suboptimal. It made more sense to read the small file's values into an array and then loop through the array for every large file's line...

Dear sir, I cannot thank you enough for your help.
I have what I have been looking for....thanks to you.

Just as a summary, this is the complete example:

Code:
gile.sh

Code:
#!/bin/bash
while read lineA
    do while read lineB
        do echo $lineA" -delimiter- "$lineB
        done < fsecond.txt
done < ffirst.txt

Code:
The OUTPUT:
> ./gile.sh
A -delimiter- 1
A -delimiter- 2
A -delimiter- 3
A -delimiter- 4
A -delimiter- 5
A -delimiter- 6
A -delimiter- 7
A -delimiter- 8
A -delimiter- 9
A -delimiter- 10
A -delimiter- 11
B -delimiter- 1
B -delimiter- 2
B -delimiter- 3
B -delimiter- 4
B -delimiter- 5
B -delimiter- 6
B -delimiter- 7
B -delimiter- 8
B -delimiter- 9
B -delimiter- 10
B -delimiter- 11
C -delimiter- 1
C -delimiter- 2
C -delimiter- 3
C -delimiter- 4
C -delimiter- 5
C -delimiter- 6
C -delimiter- 7
C -delimiter- 8
C -delimiter- 9
C -delimiter- 10
C -delimiter- 11
D -delimiter- 1
D -delimiter- 2
D -delimiter- 3
D -delimiter- 4
D -delimiter- 5
D -delimiter- 6
D -delimiter- 7
D -delimiter- 8
D -delimiter- 9
D -delimiter- 10
D -delimiter- 11
E -delimiter- 1
E -delimiter- 2
E -delimiter- 3
E -delimiter- 4
E -delimiter- 5
E -delimiter- 6
E -delimiter- 7
E -delimiter- 8
E -delimiter- 9
E -delimiter- 10
E -delimiter- 11
F -delimiter- 1
F -delimiter- 2
F -delimiter- 3
F -delimiter- 4
F -delimiter- 5
F -delimiter- 6
F -delimiter- 7
F -delimiter- 8
F -delimiter- 9
F -delimiter- 10
F -delimiter- 11

Note: I have updated first file with F and second file with 11....to see how it works.

For the real purpose of the script, first file I am going to use is about 5000 lines long, and second is 3500 lines long....just to update you with the real info.

Dear RudiC, if you think that this code can run more smoothly, I kindly ask you to offer such solution.


Best regards...and thank you very much

gandrinno.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Automate splitting of files , scp files as each split completes and combine files on target server

i use the split command to split a one terabyte backup file into 10 chunks of 100 GB each. The files are split one after the other. While the files is being split, I will like to scp the files one after the other as soon as the previous one completes, from server A to Server B. Then on server B ,... (2 Replies)
Discussion started by: malaika
2 Replies

2. Shell Programming and Scripting

Combine two text files

Hi I use Ubuntu 14.04 LTS and bash shell. I need help to write a script to combine two text files. The first file is FIRST.txt <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text... (15 Replies)
Discussion started by: my_Perl
15 Replies

3. Shell Programming and Scripting

Combine the lines from separate text files

Hi All, I have three separate text files which has only one line and i want to combine these lines in one text file which will have three lines. cat file1.txt abc cat file2.txt 1265 6589 1367 cat file3.txt 0.98 0.36 0.5 So, I want to see these three lines in the... (9 Replies)
Discussion started by: senayasma
9 Replies

4. Shell Programming and Scripting

conditionally combine text from two files into one

Hi! I'm trying to take multiple text files (6), which have text on some lines but not others, and combine them. I'd also like to make the values in one column of some of the files (files 4-6) negative. I'm trying to write a short script (see below) as I have to do this with a large number of... (2 Replies)
Discussion started by: felix.echidna
2 Replies

5. UNIX for Dummies Questions & Answers

how to combine text files

how to combine text files in new file and be separated by commas : example: f1.txt 1 2 3 f2.txt x y z f3.txt m n o i need output to be fnew.txt 1,x,m (9 Replies)
Discussion started by: takyeldin
9 Replies

6. Shell Programming and Scripting

Combine Multiple text or csv files column-wise

Hi All I am trying to combine columns from multiple text files into a single file using paste command but the record length being unequal in the different files the data is running over to the closest empty cell on the left. Please see below. What can i do to resolve this ? File 1 File... (15 Replies)
Discussion started by: venky_ibm
15 Replies

7. Shell Programming and Scripting

how to combine 2 lines in same files based on any text

hi, I want to combine two lines in same file. If the line ends with '&' it should belongs to previous line only Here i am writing example. Ex1: line 1 : return abcdefgh& line 2 : ijklmnopqr& line 3 : stuvw& line 4 : xyz output should be line 1: return abcdefghijklmnopqrstuvwxyz ... (11 Replies)
Discussion started by: spc432
11 Replies

8. Shell Programming and Scripting

How to Merge / combine / join / paste 2 text files side-by-side

I have 2 text files, both have one simple, single column. The 2 files might be the same length, or might not, and if not, it's unknown which one would be longer. For this example, file1 is longer: ---file1 Joe Bob Mary Sally Fred Elmer David ---file2 Tomato House Car... (3 Replies)
Discussion started by: cajunfries
3 Replies

9. UNIX for Dummies Questions & Answers

combine text files into one file

I need to write a shell script which combines/joins 3 text files into one file. Do i put the txt files in the same folder as my script? Here is what i have: #!/bin/bash file1=$1 file2=$2 file3=$3 out="output.txt" count=0 if then echo "$(basename $0) file1 file2 file3" ... (3 Replies)
Discussion started by: zzthejimzz
3 Replies

10. Shell Programming and Scripting

How to combine text data into one line?

The following input needs to be manipulated as follows: INPUT from file or results of command: ============start: Medium identifier : a45c0213:47eb5485:0aec:0321 Medium label : SQL Disk_11516 Location : Protected : None ... (2 Replies)
Discussion started by: rcky_mntere
2 Replies
Login or Register to Ask a Question