Merge specific columns of two files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merge specific columns of two files
# 8  
Old 09-04-2014
Combining elegance and memory consumption from both above approaches I'd propose
Code:
awk     'function findLIM(EX)   {for (X=1; X<=NF && $X!=EX; X++); return X}
         function fillTMP(S, E) {for (i=S; i<=E; i++) TMP=TMP "\t" $i }

         NR==1          {S1=1; E1=findLIM(END1)}
                        {fillTMP( S1, E1 )
                         getline < F2}
         NR==1          {E2=NF; S2=findLIM(ST2)}
                        {fillTMP( S2, E2 )
                         print substr(TMP,2); TMP="" }
        ' FS="\t" END1="$FILE1_VALUE" ST2="$FILE2_VALUE" F2="file2" file1

This User Gave Thanks to RudiC For This Post:
# 9  
Old 09-05-2014
The variation posted by Chubler_XL does work. I will test the other suggestions tomorrow.

I am looking at the possibility that I will need to modify this. At the moment, the code prints columns from the first file up to $FILE1_VALUE and then adds columns from the second file starting with $FILE2_VALUE and adding all the columns to the end of file 2.

It may be better for me to specify a range of columns for file 2, meaning it may not be all the columns to the end of the file. Something like,
Code:
$FILE1_VALUE="E199"
$FILE2_START_VALUE="E200"
$FILE2_END_VALUE="E273"

This would be columns up to E199 from the first file and E200-E273 from the second file. Would this be a difficult modification?

I can't see from Chubler_XL's code how it determines how many column to add from file 2.

I can post complete test files if that would help.

LMHmedchem
# 10  
Old 09-05-2014
Luckily just a small modification:
Code:
awk     'function findLIM(EX)   {for (X=1; X<=NF && $X!=EX; X++); return X}
         function fillTMP(S, E) {for (i=S; i<=E; i++) TMP=TMP "\t" $i }

         NR==1          {S1=1; E1=findLIM(END1)}
                        {fillTMP( S1, E1 )
                         getline < F2}
         NR==1          {S2=findLIM(ST2)
                         E2=findLIM(END2)}
                        {fillTMP( S2, E2 )
                         print substr(TMP,2); TMP="" }
        ' FS="\t" END1="$FILE1_VALUE" ST2="$FILE2_START_VALUE" END2="$FILE2_END_VALUE" F2="file2" file1

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merge files based on columns

011111123444 1234 1 20000 011111123444 1235 1 30000 011111123446 1234 3 40000 011111123447 1234 4 50000 011111123448 1234 3 50000 File2: 011111123444,Rsttponrfgtrgtrkrfrgtrgrer 011111123446,Rsttponrfgtrgtr 011111123447,Rsttponrfgtrguii 011111123448,Rsttponrfgtrgtjiiu I have 2 files... (4 Replies)
Discussion started by: vinus
4 Replies

2. Shell Programming and Scripting

Merge columns from multiple files

Hello and Good day I have a lot of files with same number of rows and columns.$2 and $3 are the same in all files . I need to merge $2,$3,$6 from first file and $6 from another files. File1: $1 $2 $3 $4 $5 $6... (8 Replies)
Discussion started by: ali.seifaddini
8 Replies

3. Shell Programming and Scripting

Merge columns on different files

Hello, I have two files that have this format: file 1 86.82 0.00 86.82 43.61 86.84 0.00 86.84 43.61 86.86 0.00 86.86 43.61 86.88 0.00 86.88 43.61 file 2 86.82 0.22 86.84 0.22 86.86 0.22 86.88 0.22 I would like to merge these two files such that the final file looks like... (5 Replies)
Discussion started by: kayak
5 Replies

4. UNIX for Dummies Questions & Answers

Merge columns from multiple files

Hi all, I've searched the web for a long time trying to figure out how to merge columns from multiple files. I know paste will append columns like so: paste file1 file2 file3 file4 file5 ... But this becomes inconvenient when you want to append a large number of files into a single file. ... (2 Replies)
Discussion started by: torchij
2 Replies

5. UNIX for Dummies Questions & Answers

How to count specific columns and merge with unique ones?

Hi. I am not sure the title gives an optimal description of what I want to do. I have several text files that contain data in many columns. All the files are organized the same way, but the data in the columns might differ. I want to count the number of times data occur in specific columns,... (0 Replies)
Discussion started by: JamesT
0 Replies

6. Shell Programming and Scripting

Merge columns of different files

Hi, I have tab limited file 1 and tab limited file 2 The output should contain common first column vales and corresponding 2nd column values; AND also unique first column value with corresponding 2nd column value of the file that contains it and 0 for the second file. the output should... (10 Replies)
Discussion started by: polsum
10 Replies

7. Shell Programming and Scripting

merge the two files which has contain columns

Hi may i ask how to accomplish this task: I have 2 files which has multiple columns first file 1 a 2 b 3 c 4 d second file 14 a 9 .... 13 b 10.... 12 c 11... 11 d 12... I want to merge the second file to first file that will looks like this ... (2 Replies)
Discussion started by: jao_madn
2 Replies

8. Shell Programming and Scripting

Merge 2 columns/remove specific spaces

Hi, I have a requirement to remove certain spaces from a table of information, but I'm unsure where to start. A typical table will be like this: ABCDE 1 Elton John 25 12 15 9 3 ABCDE 2 Oasis 29 13 4 6 9 ABCDE 3 The Rolling Stones 55 19 3 8 6The goal is to remove only the spaces between... (11 Replies)
Discussion started by: danhodges99
11 Replies

9. Shell Programming and Scripting

merge columns into one line after a specific pattern

Hi all, im a linux newbie, plz help! I have a file - box -------- Fox-2 -------- UF29 zip42 -------- zf-CW SNF2_N Heli_Z -------- Fox -------- Kel_1 box (3 Replies)
Discussion started by: sam_2921
3 Replies
Login or Register to Ask a Question