0
$\begingroup$

I have two types of files for a gene in a folder

File 1: FOS.tf.txt

ABL2
ACTN4
ADGRE5
ADIPOR2
ADRB2
ERCC4
EZR
FAS
FMO4

File 2: FOS.tt.txt

ABL2
CASP9
CCK
DDIT3
ERCC4
EZR
FAS
FMO4

After running command

join FOS.tf.txt FOS.tt.txt > FOS_final.txt

I got output file: FOS_final.txt

ABL2
ERCC4
EZR
FAS
FMO4

My question is that I have these two type of files (tf and tt) for 605 genes in a folder and I want to run join command together for all my genes and get 605 output files.

$\endgroup$
1
$\begingroup$

If all of your .tf.txt and .tt.txt files are in the same directory, the following command should work.

for tffile in $(ls *.tf.txt)
do
    prefix=$(basename $tffile .tf.txt)
    join ${prefix}.tf.txt ${prefix}.tt.txt > ${prefix}_final.txt
done
$\endgroup$
  • 1
    $\begingroup$ The basename call can be avoided by anchoring a regex with parameter expansion like so: join ${tffile} ${tffile/%.tf.txt/.tt.txt} > ${tffile/%.tf.txt/_final.txt} $\endgroup$ – Ram RS Jul 10 at 20:24
  • 1
    $\begingroup$ @RamRS Cool! I guess it's up to the individual to decide which ugly solution is less ugly! :-) $\endgroup$ – Daniel Standage Jul 11 at 13:16
  • $\begingroup$ Indeed. I wonder if $() is more performant than ${//}. $\endgroup$ – Ram RS Jul 11 at 14:14
  • $\begingroup$ @ Ram RS: I am getting this error ${tffile/%.tf.txt/_final.txt}: ambiguous redirect $\endgroup$ – Riya Jul 11 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.