Discussion:
[Help-bash] combine
Val Krem
2018-04-04 22:58:06 UTC
Permalink
Hi all,
I have two files. The first file has two columns  and the second file has about 15,000 columns and here is two samplesFile1
ABC1  zc124
ABC2  zc125
ABC3  zc126
ABC4  zc127
File2 ( has about 15, 000  columns) ABC1  1 0 1 2 1....1
ABC2  1 1 2 2 2....2
ABC3  2 2 1 0 1...,0

I want to merge the two files by the common column   (column 1 in both files) and  get the  following output
Result
Zc124 1 0 1 2 1....1
Zc125 1 1 2 2 2....2
Zc126 2 2 1 0 1...,0

Is it possible to read file 2   as a two variables? i.e.,  the first columns as one variable and the remaining columns as another available?  If this is  possible then I can combine the two files using the common  variable between the two files.

Thank you
Pierre Gaston
2018-04-05 05:45:54 UTC
Permalink
Post by Val Krem
Hi all,
I have two files. The first file has two columns and the second file has
about 15,000 columns and here is two samplesFile1
ABC1 zc124
ABC2 zc125
ABC3 zc126
ABC4 zc127
File2 ( has about 15, 000 columns) ABC1 1 0 1 2 1....1
ABC2 1 1 2 2 2....2
ABC3 2 2 1 0 1...,0
I want to merge the two files by the common column (column 1 in both
files) and get the following output
Result
Zc124 1 0 1 2 1....1
Zc125 1 1 2 2 2....2
Zc126 2 2 1 0 1...,0
Is it possible to read file 2 as a two variables? i.e., the first
columns as one variable and the remaining columns as another available? If
this is possible then I can combine the two files using the common
variable between the two files.
Thank you
It's probably better to not use bash for this but an external tool, if your
files are sorted like in your example, it could be that something like:

join file1 file2 | cut -d ' ' -f 2-

will do what you want

An alternative is to use awk.
Greg Wooledge
2018-04-05 12:42:32 UTC
Permalink
Post by Val Krem
Hi all,
I have two files. The first file has two columns  and the second file has about 15,000 columns and here is two samplesFile1
ABC1  zc124
ABC2  zc125
ABC3  zc126
ABC4  zc127
File2 ( has about 15, 000  columns) ABC1  1 0 1 2 1....1
ABC2  1 1 2 2 2....2
ABC3  2 2 1 0 1...,0
I want to merge the two files by the common column   (column 1 in both files) and  get the  following output
Result
Zc124 1 0 1 2 1....1
Zc125 1 1 2 2 2....2
Zc126 2 2 1 0 1...,0
https://mywiki.wooledge.org/BashProgramming/04

The first file can be read into an associative array that maps
"ABC1" to "Zc124" (you have to perform the capitalization at some
point, so why not now).

Then when you read the second file line-by-line, you simply replace
the first column with its value from the associative array.

Loading...