Search This Blog

A shell script to combine text files into one (each text file is a column in the new file)


#!/bin/bash

OUTPUT_FILE="$1/all_in_one_result.txt"
if [[ -f $OUTPUT_FILE ]]; then
echo "$OUTPUT_FILE already exists. You need to remove it if you want to re-run this script."
exit 1
fi
max=1
nrows=0
for f in `ls $1/*r1.txt`
do
p1=`echo $f | cut -d'_' -f1`
p2=`echo $f | cut -d'_' -f2`
i=`echo $f | cut -d'_' -f3`
prefix="${p1}_${p2}_${i}"
a[$i]=$prefix
if [[ $i -gt $max ]]; then
max=$i
fi
nrows=`cat $f | wc -l`
done


r=1
while [[ $r -le $nrows ]]
do
echo "merging line $r..."
line=""
i=1
while [[ $i -le $max ]]
do
prefix=${a[$i]}
if [[ -n "$prefix" ]]; then
for f in `ls ${prefix}_r*.txt`
do
echo " grabbing value from $f..."
value=`head -n $r $f | tail -n 1`
if [[ "$line" == "" ]]; then
line=$value
else
line="${line} ${value}"
fi
done
fi
i=$((i+1))
done
echo $line >> $OUTPUT_FILE
r=$((r+1))
done