LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-25-2013, 05:02 PM   #1
gacanepa
Member
 
Registered: May 2012
Location: San Luis, Argentina
Distribution: Debian
Posts: 205

Rep: Reputation: 27
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 [ICODE]while[/ICODE] 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.
 
Old 07-25-2013, 05:09 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,326

Rep: Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919Reputation: 919
man paste
 
Old 07-25-2013, 05:16 PM   #3
gacanepa
Member
 
Registered: May 2012
Location: San Luis, Argentina
Distribution: Debian
Posts: 205

Original Poster
Rep: Reputation: 27
Thumbs up

Quote:
Originally Posted by schneidz View Post
man paste
That worked like a charm! I knew there had to be a more efficient way than using the nested while loops.
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to concatenate files puntino Linux - Newbie 8 11-20-2012 06:11 AM
[SOLVED] bash: sort lines in 2 files so that equal lines are at the same line number... masavini Programming 10 06-21-2012 01:58 PM
commands for bash script that handles files of varying number of lines BBFeltham Linux - Newbie 1 07-26-2008 10:18 AM
Script to concatenate several files docaia Linux - General 10 02-03-2008 01:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration