Merge 3 files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merge 3 files
# 1  
Old 04-15-2016
Merge 3 files

help with a command please
i have 3 files that i need to merge in to a 4th file
using awk or sed
the files have more than 3 lines in them

file 1

Code:
AAA
BBB
CCC

file 2

Code:
AAA
BBB
CCC

file 3

Code:
AAA
BBB
CCC

i want the 4th file to look like this after merging

Code:
AAA AAA AAA
BBB BBB BBB
CCC CCC CCC


Last edited by bob123; 04-15-2016 at 04:37 PM..
# 2  
Old 04-15-2016
Hello bob123,

Could you please try following and let me know if this helps you.
Code:
awk 'FNR==1{n=n>Q?n:Q;Q=1;}{A[Q]=A[Q]?A[Q] OFS $0:$0;Q++} END{for(i=1;i<=n-1;i++){print A[i]}}' Input_file1  Input_file2  Input_file3

Output will be as follows.
Code:
AAA AAA AAA
BBB BBB BBB
CCC CCC CCC

Also in case let's say we have only 2 columns in file name Input_file2 and 3 in other 2 Input_files then also it should print all fields as they are as follows.
Code:
cat Input_file2
AAA
BBB
cat Input_file1
AAA
BBB
CCC
cat Input_file3
AAA
BBB
CCC

After running code we would get following.
Code:
awk 'FNR==1{n=n>Q?n:Q;Q=1;}{A[Q]=A[Q]?A[Q] OFS $0:$0;Q++} END{for(i=1;i<=n-1;i++){print A[i]}}'  Input_file1  Input_file2  Input_file3
AAA AAA AAA
BBB BBB BBB
CCC CCC

You could re-direct this above command's output to a file named Input_file4 too.
EDIT: Adding a non-one liner form of solution too here for same.
Code:
awk 'FNR==1{
                n=n>Q?n:Q;
                Q=1;
           }
           {
                A[Q]=A[Q]?A[Q] OFS $0:$0;
                Q++
           }
     END   {
                for(i=1;i<=n-1;i++){
                                        print A[i]
                                   }
           }
    ' Input_file1  Input_file2  Input_file3

Thanks,
R. Singh

Last edited by RavinderSingh13; 04-15-2016 at 04:48 PM.. Reason: Added a non-one liner form of solution.
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 04-15-2016
@RavinderSingh13
thanks for your expertise

this works great Smilie

Code:
awk 'FNR==1{n=n>Q?n:Q;Q=1;}{A[Q]=A[Q]?A[Q] OFS $0:$0;Q++} END{for(i=1;i<=n-1;i++){print A[i]}}'  Input_file1  Input_file2  Input_file3

# 4  
Old 04-15-2016
Hi.

For the data sets, data[123], as noted in #2:
Code:
$ paste data*
AAA     AAA     AAA
BBB     BBB     BBB
        CCC     CCC

On a system like:
Code:
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.3 (jessie) 
paste (GNU coreutils) 8.23

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to merge two files?

Dear Frens, I have two files and need to merge into one file. Like File_1: Field1 Field2 1 4 File_2: Field1 Field2 3 5 I need one single output as File_1: Field1 Field2 1 4 3 5 This means taking header from either file. (8 Replies)
Discussion started by: manisha_singh
8 Replies

2. Shell Programming and Scripting

Merge files and generate a resume in two files

Dear Gents, Please I need your help... I need small script :) to do the following. I have a thousand of files in a folder produced daily. I need first to merge all files called. txt (0009.txt, 0010.txt, 0011.txt) and and to output a resume of all information on 2 separate files in csv... (14 Replies)
Discussion started by: jiam912
14 Replies

3. Shell Programming and Scripting

Checking in a directory how many files are present and basing on that merge all the files

Hi, My requirement is,there is a directory location like: :camp/current/ In this location there can be different flat files that are generated in a single day with same header and the data will be different, differentiated by timestamp, so i need to verify how many files are generated... (10 Replies)
Discussion started by: srikanth_sagi
10 Replies

4. UNIX for Dummies Questions & Answers

Merge files

Hi, I would like to know how can I merge files based on their coordinates, but mantaining the score of each file in the output file like: Note: 1st column is for chromosome, 2nd for start, 3rd for end of segment, 4th for score file1: 1 200 300 20 1 400 500 30 file2: 1 200 350 30 1... (1 Reply)
Discussion started by: fadista
1 Replies

5. Shell Programming and Scripting

MERGE FILES

Hi all! How could I merge all the text files (in format xml) of a single folder, after having deleted from each of them all the text from its beginning up to a specific string: "<body>" ? Thanks a lot! mjomba (4 Replies)
Discussion started by: mjomba
4 Replies

6. Shell Programming and Scripting

Merge 2 files

Hi , This might be the stupidest question I am asking, but I am struck with this problem. I am trying to merge 2 files, file1 has header and file2 has contents. while I merge them , it merges from the 1st line of file1. for ex: file1 col1|col2|col3| file2 123|234|456|... (2 Replies)
Discussion started by: rashmisb
2 Replies

7. Shell Programming and Scripting

How can i merge these two files into several...

Given are File A and File B File A has for example 5 lines: AAA BBB CCC DDD EEE File B has 3 lines: 111 222 333 How can i merge A and B into: 111 222 333 AAA (first line from A) then a new file: (4 Replies)
Discussion started by: Y-T
4 Replies

8. Shell Programming and Scripting

Merge files of differrent size with one field common in both files using awk

hi, i am facing a problem in merging two files using awk, the problem is as stated below, file1: A|B|C|D|E|F|G|H|I|1 M|N|O|P|Q|R|S|T|U|2 AA|BB|CC|DD|EE|FF|GG|HH|II|1 .... .... .... file2 : 1|Mn|op|qr (2 Replies)
Discussion started by: shashi1982
2 Replies

9. Shell Programming and Scripting

merge files

Hi, i have the files f1 and f2 like: files f1: c1 a1 c2 a2 c3 a3 file f2: c1 b1 c2 b2 c3 b3 i want merge the f1 and f2 file to f3 file like: c1 a1 b1 c2 a2 b3 c3 a3 b3........ .... . . please help me onthis..... (5 Replies)
Discussion started by: koti_rama
5 Replies

10. Shell Programming and Scripting

How to merge files

Hello guys, I gotta question, i have a lot of log files (simple text) and i need to merge them in group of 10 files, one next to the other, that have sense? For example, i have the files: File1 File2 File3 File4 . . File100 I need to merge the contents of each file into a new file... (3 Replies)
Discussion started by: lestat_ecuador
3 Replies
Login or Register to Ask a Question