Combining column results of two or more files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combining column results of two or more files
# 1  
Old 10-11-2011
Combining column results of two or more files

I have a number of result files (the number of files varies) that I need to combine into a new file with new columns for each data line. If one file does not have a result for any row it is set to zero. Is this possible? The ID and Name will always match in a row (first two columns, if it is present).

File 1 (sample1.txt)

Code:
1.2.3 general 0.69360
1.2.3.1 unclassified  0.00651
1.2.4 novel 0.00345
1.2.4.2 SM_D3_B5 0.00223

File 2 (sample2.txt)

Code:
1.2.3 general 0.56221
1.2.4 novel 0.00782
1.2.4.2 SM_D3_B5 0.00240
1.2.5 known 0.00230

Result file (to be named result.txt)

Code:
ID Name sample1 sample2
1.2.3 general 0.69360 0.56221
1.2.3.1 unclassified  0.00651 0
1.2.4 novel 0.00345 0.00782
1.2.4.2 SM_D3_B5 0.00223 0.00240
1.2.5 known 0 0.00230

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by radoulov; 10-11-2011 at 05:55 PM..
# 2  
Old 10-11-2011
Code:
awk > result.txt 'END {
  printf "%s", ("ID" FS "Name" FS)
  for (i = 0; ++i <= cf;)
    printf "%s", (f[i] (i < cf ? FS : RS))
  for (i = 0; ++i <= ckc;) {
    printf "%s", (sk[i] FS)
    for (j = 0; ++j <= cf;)
      printf "%s", ((ck[f[j], sk[i]] ? ck[f[j], sk[i]] : 0) (j < cf ? FS : RS))
    }      
  }
{
  ck[FILENAME, $1 FS $2] = $3
  FNR == 1 && f[++cf] = FILENAME
  _t[$1, $2]++ || sk[++ckc] = $1 FS $2 
  }' sample*.txt

# 3  
Old 10-11-2011
I renamed my files to be sample1.txt sample2.txt (they were actually things like ONA.txt 125.txt OTS.txt 499.txt) and put them in a separate directory and then ran the command.
It worked nicely.

Thanks!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combining 2 files

i am having 2 files like this file 1 1, 2, 3, 4, file2 5, 6, 7, 8, what i want do is like this i want to put all the contents for file 2 after file 1,means adding column in file1 (5 Replies)
Discussion started by: sagar_1986
5 Replies

2. Shell Programming and Scripting

Combining multiple column files into one with file name as first row

Hello All, I have several column files like this $cat a_b_s1.xls 1wert 2tg 3asd 4asdf 5asdf $cat c_d_s2.xls 1wert 2tg 3asd 4asdf 5asdf desired put put $cat combined.txt s1 s2 (2 Replies)
Discussion started by: avatar_007
2 Replies

3. Shell Programming and Scripting

column the results

hello I'm writing a bash script and the thing is I want the results that come from the command call to be put the one under the other in ONE column.Instead they're put the one next to the other in different columns.the command is for loop do command | awk '{print $1,$2,$3} |... (1 Reply)
Discussion started by: vlm
1 Replies

4. UNIX for Dummies Questions & Answers

Need Help in reading N days files from a Directory & combining the files

Hi All, Request your expertise in tackling one requirement in my project,(i dont have much expertise in Shell Scripting). The requirement is as below, 1) We store the last run date of a process in a file. When the batch run the next time, it should read this file, get the last run date from... (1 Reply)
Discussion started by: dsfreddie
1 Replies

5. Shell Programming and Scripting

Combining two single column files side-by-side

Hi, I am looking for a sed/awk script to join two large (~300 M) single column files (one is sorted and the other is not sorted) side-by-side. I have a shell script but its taking ages to do the task so looking for an optimized fast solution. The two files look like: File1 (sorted) a1... (1 Reply)
Discussion started by: sajal.bhatia
1 Replies

6. UNIX for Dummies Questions & Answers

Combining three space delimited files by column

I have three space delimited files, how do I combine them by column in Unix? Thanks! (3 Replies)
Discussion started by: evelibertine
3 Replies

7. Shell Programming and Scripting

Display echo results in three column

Dear Friends, I have my command output which displays on one row and values are now scrollable (vertical) 3 pages. How do i display those output in three column so that i no need to scroll? Example: dcadd$cat components 1.Caluculator 2.Diary ... ... 50.Mobile 51.Battery .. ...... (12 Replies)
Discussion started by: baluchen
12 Replies

8. UNIX for Dummies Questions & Answers

combining two files

Hi Gurus, I have 2 files: File1 Filename1 xx Filename1 yy Filename1 Total Filename2 xx Filename2 yy Filename2 zz Filename2 Total Filename3 xx Filename3 Total and File2: Filename1 10296 xxx Date: 09/01/08 Filename2 10296 xxx Date: 09/05/08... (36 Replies)
Discussion started by: rock1
36 Replies

9. Shell Programming and Scripting

How do I transpose a column of results to a row

Hi, Can anyone advise me what command I could use to display the results of the following command as a row. Thanks Gareth (6 Replies)
Discussion started by: m223464
6 Replies

10. UNIX for Dummies Questions & Answers

Combining files

Hi, is there a way to combine 2 files together, joining line 1 from file A with line 1 from file B, line 2 from A with line 2 from B etc. File A File B 1 4 2 5 3 6 Combined result = File C 14 25 36 (2 Replies)
Discussion started by: Enda Martin
2 Replies
Login or Register to Ask a Question