[Solved] Script to concatenate 2 files with the same number of lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] Script to concatenate 2 files with the same number of lines
# 1  
Old 07-25-2013
[Solved] Script to concatenate 2 files with the same number of lines

Hi everyone,
I have two files, namely:
file1:
Code:
file1Col1Row1;file1Col2Row1;file1Col3Row1
file1Col1Row2;file1Col2Row2;file1Col3Row2
file1Col1Row3;file1Col2Row3;file1Col3Row3

file2:
Code:
file2Col1Row1;file2Col2Row1;file2Col3Row1
file2Col1Row2;file2Col2Row2;file2Col3Row2
file2Col1Row3;file2Col2Row3;file2Col3Row3

I need to concatenate both files line by line so that the output will be:
Code:
file1Col1Row1;file1Col2Row1;file1Col3Row1;file2Col1Row1;file2Col2Row1;file2Col3Row1
file1Col1Row2;file1Col2Row2;file1Col3Row2;file2Col1Row2;file2Col2Row2;file2Col3Row2
file1Col1Row3;file1Col2Row3;file1Col3Row3;file2Col1Row3;file2Col2Row3;file2Col3Row3

The only way I can think of is using 2 nested while loops that read from each file and output to another file, but I don't know if there's a more efficient way?
Any ideas will be more than welcome. Thanks in advance.

---------- Post updated at 07:18 PM ---------- Previous update was at 07:02 PM ----------

I found the answer:
Code:
gacanepa@Gabriel-PC ~/stuff/scripts/bash $ cat file1
file1Col1Row1;file1Col2Row1;file1Col3Row1
file1Col1Row2;file1Col2Row2;file1Col3Row2
file1Col1Row3;file1Col2Row3;file1Col3Row3
gacanepa@Gabriel-PC ~/stuff/scripts/bash $ cat file2
file2Col1Row1;file2Col2Row1;file2Col3Row1
file2Col1Row2;file2Col2Row2;file2Col3Row2
file2Col1Row3;file2Col2Row3;file2Col3Row3
gacanepa@Gabriel-PC ~/stuff/scripts/bash $ paste -d";" file1 file2
file1Col1Row1;file1Col2Row1;file1Col3Row1;file2Col1Row1;file2Col2Row1;file2Col3Row1
file1Col1Row2;file1Col2Row2;file1Col3Row2;file2Col1Row2;file2Col2Row2;file2Col3Row2
file1Col1Row3;file1Col2Row3;file1Col3Row3;file2Col1Row3;file2Col2Row3;file2Col3Row3

---------- Post updated at 07:20 PM ---------- Previous update was at 07:18 PM ----------

I need the help of a moderator here to mark this thread as solved. I believe I can't do it since I found the answer before any one replied to the post.

Last edited by Don Cragun; 07-26-2013 at 12:12 AM.. Reason: Note in thread Title that issue has been solved.
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 number of lines for files in directory, also print number of unique lines

I have a directory of files, I can show the number of lines in each file and order them from lowest to highest with: wc -l *|sort 15263 Image.txt 16401 reference.txt 40459 richtexteditor.txt How can I also print the number of unique lines in each file? 15263 1401 Image.txt 16401... (15 Replies)
Discussion started by: spacegoose
15 Replies

2. Shell Programming and Scripting

Concatenate select lines from multiple files

I have about 6000 files of the following format (three simplified examples shown; actual files have variable numbers of columns, but the same number of lines). I would like to concatenate the ID (*Loc*) and data lines, but not the others, as shown below. The result would be one large file (or... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

3. Shell Programming and Scripting

[Solved] Counting The Number of Lines Between Values with Multiple Variables

Hey everyone, I have a bunch of lines with values in field 4 that I am interested in. If these values are between 1 and 3 I want it to count all these values to all be counted together and then have the computer print out LOW and the number of lines with those values in between 1 and 3,... (2 Replies)
Discussion started by: VagabondGold
2 Replies

4. UNIX for Dummies Questions & Answers

Writing a script to print the number of lines in multiple files

Hi I have 1000 files labelled data1.txt through data1000.txt. I want to write a script that prints out the number of lines in each txt file and outputs it in the following format: Column 1: number of data file (1 through 1000) Column 2: number of lines in the text file Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

5. Shell Programming and Scripting

Concatenate lines with unique string AND number

In Bash using AWK or sed I need to convert the following file: ... numitem_tab0 =<p>1 KEYWORD</p><p>2 KEYWORD</p><p>3 KEYWORD</p><p>4 KEYWORD</p><p>5 KEYWORD</p>...<p>25 KEYWORD</p> subitem_tab0 =<p></p><p></p> ... numitem_tab6 =<p>1 KEYWORD</p><p>2 KEYWORD</p><p>3 KEYWORD</p><p>4 KEYWORD</p>... (2 Replies)
Discussion started by: pioavi
2 Replies

6. UNIX for Advanced & Expert Users

Concatenate lines in file shell script

Hi colleagues, I have a file in this format. "/cccc/pppp/dddd/ggg/prueba.txt". ERROR" THE error bbbbbbbbbb finish rows. "/kkkk/mmmm/hhhh/jjj/ejemplo.txt". ERROR This is other error rows.I need my file in this format. "/cccc/pppp/dddd/ggg/prueba.txt". ERROR" THE error bbbbbbbbbb finish rows.... (3 Replies)
Discussion started by: systemoper
3 Replies

7. Shell Programming and Scripting

[Solved] How to concatenate files in subdirectories?

I have many log files in various subdirectories of a parent directory. The directory structure e something like this: Parent directory: /home/linus/logfiles/ This parent directory has many subdirectories, like this: .../logfiles/jack .../logfiles/helen .../logfiles/sue... (13 Replies)
Discussion started by: zetetic
13 Replies

8. Shell Programming and Scripting

perl script on how to count the total number of lines of all the files under a directory

how to count the total number of lines of all the files under a directory using perl script.. I mean if I have 10 files under a directory then I want to count the total number of lines of all the 10 files contain. Please help me in writing a perl script on this. (5 Replies)
Discussion started by: adityam
5 Replies

9. Shell Programming and Scripting

Script to split files based on number of lines

I am getting a few gzip files into a folder by doing ftp to another server. Once I get them I move them to another location .But before that I need to make sure each gzip is not more than 5000 lines and split it up . The files I get are anywhere from 500 lines to 10000 lines in them and is in gzip... (4 Replies)
Discussion started by: gubbu
4 Replies

10. Shell Programming and Scripting

Script to concatenate several files

I need a script to concatenate several files in one step, I have 3 header files say file.S, file.X and file.R, I need to concatenate these 3 header files to data files, say file1.S, file1.R, file1.X so that the header file "file.S" will be concatenated to all data files with .S extentions and so on... (3 Replies)
Discussion started by: docaia
3 Replies
Login or Register to Ask a Question