Visit Our UNIX and Linux User Community


Adding loads of columns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Adding loads of columns
# 1  
Old 11-03-2009
Adding loads of columns

Hi All,

I've got file1 like this:
Code:
aaa bbb ccc
ddd eee fff
ggg hhh kkk
ppp mmm nnn

and file 2 like this:
Code:
aaa qqq www
ddd fff ggg
ggg sss zzz
ppp vvv yyy

and file 3 like this:
Code:
aaa 
ggg
ppp

I need to match the first column of file3 and file1, then add the rest of the file 1 to the file3. The result would be:
Code:
aaa bbb ccc
ggg hhh kkk
ppp mmm nnn

Then again match the first column and add the rest of the file2. The result would be:
Code:
aaa bbb ccc qqq www
ggg hhh kkk sss zzz
ppp mmm nnn vvv yyy

I've got many files and each of them has matching patterns in first column and then loads of columns that I need to all add into one file. Hope this is not too confusing :-)

Any help will be GREATLY appreciated! It doesn't need to be a big loop or anything, I don't mind if someone could help me with the first step only...

Many thanks!
# 2  
Old 12-22-2009
Have you searched this forum for "matching columns"? There are many threads dealing with just this type of problem. My personal suggestion would be to use a database, since it will give you fault tolerant processing and you have an additional reason to use a database, which is the large number of files. Hope this helps.
# 3  
Old 12-23-2009
use the "sort" command, no options, to sort all the files.

then use the "join" command to put the files together.

ta da.

add more files in sequence.

Code:
sort a > a.sorted
sort b > b.sorted

join a.sorted b.sorted > ab.joined

# 4  
Old 02-24-2010
join file1 file2 >file4
join file3 file4 > file3.out
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #226
Difficulty: Easy
According to NetMarketShare, in September 2019 Android had less than a two-to-one lead over iOS in worldwide mobile device operating system market share.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Adding columns from 2 files with variable number of columns

I have two files, file1 and file2 who have identical number of rows and columns. However, the script is supposed to be used for for different files and I cannot know the format in advance. Also, the number of columns changes within the file, some rows have more and some less columns (they are... (13 Replies)
Discussion started by: maya3
13 Replies

2. Shell Programming and Scripting

Adding columns with values dependent on existing columns

Hello I have a file as below chr1 start ref alt code1 code2 chr1 18884 C CAAAA 2 0 chr1 135419 TATACA T 2 0 chr1 332045 T TTG 0 2 chr1 453838 T TAC 2 0 chr1 567652 T TG 1 0 chr1 602541 ... (2 Replies)
Discussion started by: plumb_r
2 Replies

3. Shell Programming and Scripting

Help with ask adding columns

Hello, I am using AWK in UBUNTU 12.04. I have a dataset as follows: 1 2 12 1 4 1 4 1 7 9 4 6 1 2 4 5 7 8 45 7 4 5 7 5 What I want to do is to add the values of some columns to each other and print it in the same file as the new column while omitting the previous two columns to have... (3 Replies)
Discussion started by: Homa
3 Replies

4. Shell Programming and Scripting

Adding columns of time

Hello all, I'm in the process of writing a script, and I need to be able to add columns of time in the following format (time elapsed Net Backup logs): 000:01:03 000:00:58 000:00:49 Does anyone have a way of converting and/or adding timestamps such as these accurately? Thanks in... (9 Replies)
Discussion started by: LinuxRacr
9 Replies

5. UNIX for Dummies Questions & Answers

Perl - adding columns to file

I have a file in which I need to add more columns to based on a key in the first file: File1 key1,abc,123, key2,def,456, key3,ghi,789, File2 key2,zyx,111,qqq, key3,yuu,222,www, key1,pui,333,eee, key4,xxx,999,rrr, I would like to create the following output: Output (1 Reply)
Discussion started by: WongSifu
1 Replies

6. Shell Programming and Scripting

sorting and adding columns

i have a file with two columns, and i want to uniquely sort the values in fist column and add the corresponding values in the second columns eg file a contents tom 200 john 300 sow 500 tom 800 james 50 sow 300 output shpould be in file b as tom 1000 john 300 sow 800 james 50 (0 Replies)
Discussion started by: dealerso
0 Replies

7. Shell Programming and Scripting

Adding columns of two files

Hello everyone, I have two files containing 6 columns and thousands of rows. I want to add them (i.e. first column of first file + first column of second file and so on) and print the output in a third file. Can you please help me. Thanks a lot (7 Replies)
Discussion started by: chandra321
7 Replies

8. Shell Programming and Scripting

adding columns

Hey everyone! I have a need to add 2 files together as columns. For instance, I have one file that has several rows of data and I want to take data from another file and add Line 1 to the end of Line1 in the first file file1 line1.........file2 line1 file1 line2.........file2 line2... (12 Replies)
Discussion started by: Kelam_Magnus
12 Replies

9. UNIX for Dummies Questions & Answers

Adding columns to a file

I want to select the first column from a daily file called foo.csv. The result is written to file foo.txt. Currently the following script is used for that: cut -d, -f 1 foo.csv > foo.txt A typical result would yield : A12 A45 B11 B67 What needs to happen in addition is that two columns... (5 Replies)
Discussion started by: figaro
5 Replies

10. UNIX for Advanced & Expert Users

Adding columns in csv

I have the following data in FILE1.CSV: code: Amount1: Amount2: xxxxx ,, 200 ,,400 yyxxa ,,200 bbcgu ,,2500 ,,300 i want to be able to produce the following FILE2.CSV: code: Amount xxxxx ,, 600... (7 Replies)
Discussion started by: chachabronson
7 Replies

Featured Tech Videos