Combining columns from multiple files into one single output file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combining columns from multiple files into one single output file
# 1  
Old 03-27-2013
Combining columns from multiple files into one single output file

Hi,
I have 3 files with one column value as shown
File: a.txt
------------
Data_a1
Data_a2
File2: b.txt
------------
Data_b1
Data_b2
Data_b3
Data_b4

File3: c.txt
------------
Data_c1
Data_c2
Data_c3
Data_c4
Data_c5
Data_c6
Requirement is to merge all 3 columns delimitted by ":" into a new file called output.txt
Output File: output.txt
-----------------------
Data_a1Smilieata_b1Smilieata_c1
Data_a2Smilieata_b2Smilieata_c2
Smilieata_b3Smilieata_c3
Smilieata_b4Smilieata_c4
:Smilieata_c5
:Smilieata_c6
Similarly, I may have 4 files as input and need to combine them into new file.
File: a.txt
-----------
Data_a1
Data_a2
File2: b.txt
------------
Data_b1
Data_b2
Data_b3
File3: c.txt
------------
Data_c1
Data_c2
Data_c3
Data_c4
File4: d.txt
------------
Data_d1
Data_d2
Data_d3
Data_d4
Data_d5

Output File: output.txt
------------------------
Data_a1Smilieata_b1Smilieata_c1Smilieata_d1
Data_a2Smilieata_b2Smilieata_c2Smilieata_d2
Smilieata_b3Smilieata_c3Smilieata_d3
:Smilieata_c4Smilieata_d4
:::Smilieata_d5

Thanks in advance for your help.
Thanks,
Venkat

---------- Post updated at 11:01 PM ---------- Previous update was at 10:59 PM ----------

I am submitting again because of smile icon came for colon D characters...hence changed the data....

Hi,
I have 3 files with one column value as shown
File: a.txt
------------
Information_a1
Information_a2
File2: b.txt
------------
Information_b1
Information_b2
Information_b3
Information_b4

File3: c.txt
------------
Information_c1
Information_c2
Information_c3
Information_c4
Information_c5
Information_c6
Requirement is to merge all 3 columns delimitted by ":" into a new file called output.txt
Output File: output.txt
-----------------------
Information_a1:Information_b1:Information_c1
Information_a2:Information_b2:Information_c2
:Information_b3:Information_c3
:Information_b4:Information_c4
::Information_c5
::Information_c6
Similarly, I may have 4 files as input and need to combine them into new file.
File: a.txt
-----------
Information_a1
Information_a2
File2: b.txt
------------
Information_b1
Information_b2
Information_b3
File3: c.txt
------------
Information_c1
Information_c2
Information_c3
Information_c4
File4: d.txt
------------
Information_d1
Information_d2
Information_d3
Information_d4
Information_d5

Output File: output.txt
------------------------
Information_a1:Information_b1:Information_c1:Information_d1
Information_a2:Information_b2:Information_c2:Information_d2
:Information_b3:Information_c3:Information_d3
::Information_c4:Information_d4
::::Information_d5

Thanks in advance for your help.
Thanks,
Venkat
# 2  
Old 03-27-2013
Try paste command
# 3  
Old 03-27-2013
I tried Paste but it is not giving the result as expected...

Output File: output.txt
-----------------------
Information_a1:Information_b1:Information_c1
Information_a2:Information_b2:Information_c2
:Information_b3:Information_c3
:Information_b4:Information_c4
::Information_c5
::Information_c6

But Paste is giving:

Output File: output.txt
-----------------------
Information_a1:Information_b1:Information_c1
Information_a2:Information_b2:Information_c2
Information_b3:Information_c3
Information_b4:Information_c4
Information_c5
Information_c6
# 4  
Old 03-27-2013
Code:
paste -d':' f1 f2 f3 | awk -F: -v No_of_input_files=3 ' { print substr("::",1,No_of_input_files-NF) $0 } '
Information_a1:Information_b1:Information_c1
Information_a2:Information_b2:Information_c2
:Information_b3:Information_c3
:Information_b4:Information_c4
::Information_c5
::Information_c6

# 5  
Old 03-27-2013
Thanks alot for the code you provided.
This code worked fine.

Thanks again for your help.

Venkat
# 6  
Old 03-27-2013
Similarly you can use 'join' command
# 7  
Old 03-27-2013
Thanks Sanjeev for responding.
Paste worked, hence did not even try join command.
This is just for learning purpose, can you please share the code with "join". I tried but was not getting the desired result.

Thanks,
Venkat
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Output file name and file contents of multiple files to a single file

I am trying to consolidate multiple information files (<hostname>.Linux.nfslist) into one file so that I can import it into Excel. I can get the file contents with cat *Linux.nfslist >> nfslist.txt. I need each line prefaced with the hostname. I am unsure how to do this. --- Post updated at... (5 Replies)
Discussion started by: Kentlee65
5 Replies

2. Shell Programming and Scripting

Combining certain columns of multiple files into one file

Hello Unix gurus, I have a large number of files (say X) each containing two columns of data and the same number of rows. I would like to combine these files to create a unique merged file containing X columns corresponding to the second column of each file (with a bonus of having the first... (3 Replies)
Discussion started by: ksennin
3 Replies

3. Shell Programming and Scripting

Join two files combining multiple columns and produce mix and match output

I would like to join two files when two columns in each file matches with each other and then produce an output when taking multiple columns. Like I have file A 1234,ABCD,23,JOHN,NJ,USA 2345,ABCD,24,SAM,NY,USA 5678,GHIJ,24,TOM,NY,USA 5678,WXYZ,27,MAT,NJ,USA and file B ... (2 Replies)
Discussion started by: mady135
2 Replies

4. Shell Programming and Scripting

Compare columns of multiple files and print those unique string from File1 in an output file.

Hi, I have multiple files that each contain one column of strings: File1: 123abc 456def 789ghi File2: 123abc 456def 891jkl File3: 234mno 123abc 456def In total I have 25 of these type of file. (5 Replies)
Discussion started by: owwow14
5 Replies

5. Shell Programming and Scripting

Using a single "find" cmd to search for multiple file types and output individual files

Hi All, I am new here but I have a scripting question that I can't seem to figure out with the "find" cmd. What I am trying to do is to only have to run a single find cmd parsing the directories and output the different file types to induvidual files and I have been running into problems.... (3 Replies)
Discussion started by: swaters
3 Replies

6. UNIX for Dummies Questions & Answers

Need help combining txt files w/ multiple lines into csv single cell - also need data merge

:confused:Hello -- i just joined the forums. I am a complete noob -- only about 1 week into learning how to program anything... and starting with linux. I am working in Linux terminal. I have a folder with a bunch of txt files. Each file has several lines of html code. I want to combine... (2 Replies)
Discussion started by: jetsetter
2 Replies

7. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

8. Programming

PERL:Combining multiple lines to single line

Hi All I need a small help for the below format in making a small script in Perl or Shell. I have a file in which a single line entries are broken into three line entries. Eg: I have a pen and notebook. All i want is to capture in a single line in a separate file. eg: I have a pen and... (4 Replies)
Discussion started by: Kalaiela
4 Replies

9. Shell Programming and Scripting

Combining columns from multiple files to one file

I'm trying to combine colums from multiple file to a single file but having some issues, appreciate your help. The filenames are the same except for the extension, path1.m0 --------- a b c d e f g h i path1.m1 --------- m n o p q r s t u File names are path1.m The... (3 Replies)
Discussion started by: rkmca
3 Replies

10. Shell Programming and Scripting

Filtering issues with multiple columns in a single file

Hi, I am new to unix and would greatly appreciate some help. I have a file containing multiple colums containing different sets of data e.g. File 1: John Ireland 27_December_69 Mary England 13_March_55 Mike France 02_June_80 I am currently using the awk... (10 Replies)
Discussion started by: crunchie
10 Replies
Login or Register to Ask a Question