Combine columns from multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combine columns from multiple files
# 1  
Old 05-03-2012
Combine columns from multiple files

Can anybody help on the script to combine/concatenate columns from multiple files

input1
HTML Code:
4 135
5 185
6 85
11 30
16 72
17 30
21 52
22 76
input2
HTML Code:
2 50
4 50
6 33
8 62
10 25
12 46
14 42
15 46
output
HTML Code:
nr input1 input2
2          50
4  135     50
5  185
6  85      33
8          62
10         25
11  30
12         46
14         42
15         46
16  72
17  30
21  52
22  76
# 2  
Old 05-03-2012
look into the join command
Code:
join -a 1 -a 2 file1 file2

# 3  
Old 05-03-2012
Here is a try in perl (works for more than two input files, too):
Save code as comb.pl and call with: perl comb.pl input1 input2 ...
Code:
#!/usr/bin/perl
exit if $#ARGV < 0;
for ($i=0; $i<=$#ARGV; $i++) {
  push @init, "";
}
$n=0;
push @header, "nr";
while ($ARG = shift) {
  push @header, $ARG;
  open(IN, $ARG) || die "cannot open $ARG";
  while (<IN>) {
    chomp;
    split;
    $entry{@_[0]} = [ @init ] if !defined($entry{@_[0]});
    $entry{@_[0]}[$n] = @_[1];
  }
  $n++;
}
for ($i=0; $i<=$n; $i++) {
  printf("%-10s", $header[$i]);
}
print "\n";
foreach $key (sort {$a <=> $b} keys(%entry)) {
  printf("%-10s", $key);
  for ($i=0; $i<$n; $i++) {
    printf("%-10s",${entry{$key}}[$i]);
  }
  print "\n";
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combine and complete multiple CSV files based on 1 parameter

I have to create a new CSV file based on the value listed on the 3rd column from different CSV files. This is what I need: 1. I should substitute the first column from each file, excluding the headers, with the file name InputXX. 2. Then, I need to look for rows with 0 on the third column in... (7 Replies)
Discussion started by: Xterra
7 Replies

2. Shell Programming and Scripting

Combine columns from many files but keep them aligned in columns-shorter left column issue

Hello everyone, I searched the forum looking for answers to this but I could not pinpoint exactly what I need as I keep having trouble. I have many files each having two columns and hundreds of rows. first column is a string (can have many words) and the second column is a number.The files are... (5 Replies)
Discussion started by: isildur1234
5 Replies

3. Shell Programming and Scripting

Combine Multiple Files into Single One File One after other

I am trying to combine 4 .dat files into one single Output file Inputs are:- file123.dat, file256.dat, file378.dat & file490 Expected Output:- FileName=file1 {text from file1} EOF {blank line} FileName=file2 {text from file2} EOF {blank line} FileName=file3 {text from file3} EOF... (4 Replies)
Discussion started by: lancesunny
4 Replies

4. UNIX for Dummies Questions & Answers

Combine columns from 100 files with same structure

Hi, I have a bunch of files with the following format. PUR.1.9 30910 0.024 0.926 0.050 36587 0.024 0.927 0.049 91857 0.023 0.928 0.049 105797 0.024 0.927 0.049 146659 0.024 0.927 0.049 152695 0.024 0.927 0.049 192118 0.022 0.930 0.048 193310 0.018 0.936 0.046 PUR.2.9 30910 0.028... (6 Replies)
Discussion started by: genehunter
6 Replies

5. UNIX for Dummies Questions & Answers

How to combine 2 files with 6 columns?

This may seem obvious but I am having problems doing this as columns get converted to rows when i try to write a script. I have 2 files text1.txt and text2.txt each of which have 6 columns of numbers separated by a space. I need to combine the 2 files so that the output file text3.txt maintains... (2 Replies)
Discussion started by: tgoldstone
2 Replies

6. Emergency UNIX and Linux Support

Combine multiple Files into one big file

Hi Ppl, I have a requirement like i will be getting files of huge size daily and if the file size is so huge ,the files will be split into many parts and sent.The first file will have the header details followed by detail records and the consecutive files will have detail records and the last... (11 Replies)
Discussion started by: ganesh_248
11 Replies

7. Shell Programming and Scripting

How to combine 2 files into 1 file with 2 columns

Hi Guys, I want to combine 2 files and and put together in 1 file and make two columns out it. See below desired output. Any help will be much appreciated. inputfile1.txt 12345 67890 24580 inputfile2.txt AAAAA BBBBB CCCCC (11 Replies)
Discussion started by: pinpe
11 Replies

8. Shell Programming and Scripting

combine multiple files by column into one files already sorted!

I have multiple files; each file contains a certain data in a column view simply i want to combine all those files into one file in columns example file1: a b c d file 2: 1 2 3 4 file 3: G (4 Replies)
Discussion started by: ahmedamro
4 Replies

9. UNIX for Dummies Questions & Answers

Combine multiple files with common string into one new file.

I need to compile a large amount of data with a common string from individual text files throughout many directories. An example data file is below. I want to search for the following string, "cc_sectors_1" and combine all the data from each file which contains this string, into one new... (2 Replies)
Discussion started by: GradStudent2010
2 Replies

10. Shell Programming and Scripting

Combine multiple columns from multiple files

Hi there, I was wondering if someone can help me with this. I am trying the combine multiple columns from multiple files into one file. Example file 1: c0t0d0 c0t2d0 # hostname vgname c0t0d1 c0t2d1 # hostname vgname c0t0d2 c0t2d2 # hostname vgname c0t1d0 c0t3d0 # hostname vgname1... (5 Replies)
Discussion started by: martva
5 Replies
Login or Register to Ask a Question