Reputation: 137
I am trying to merge two intput files to produce the below output data using the below script but don't know how to add results into a the second column:
#!/bin/bash
while read gene number
do
while read gene2 type
do
if [ "$gene" = "$gene2" ]
then
echo -e "$gene\t$number\t$type" >>output.txt
elif [ "$gene" = "$type" ]
then
??? add the numbers in the second column >>output.txt
fi
done<in2.txt
done<in1.txt
here are my input files:
in1.txt:
gene1 30
gene2 20
f 1
e 2
in2.txt:
gene1 f
gene2 e
desired output:
output.txt:
gene1 30 f 1
gene2 20 e 2
Upvotes: 1
Views: 309
Reputation: 88583
With bash
and two associative arrays (in1, in2):
#!/bin/bash
declare -A in1 in2
# read both files in associative arrays
while IFS=$'\t' read -r key value; do in1[$key]="$value"; done < in1.txt
while IFS=$'\t' read -r key value; do in2[$key]="$value"; done < in2.txt
# if necessary show both associative arrays
#declare -p in1 in2
# iterate over keys of associative array in2 and output data
for i in "${!in2[@]}"; do
echo -e "$i\t${in1[$i]}\t${in2[$i]}\t${in1[${in2[$i]}]}"
done
Output:
gene2 20 e 2 gene1 30 f 1
Upvotes: 1