0

I have two principal data files (datafile1 and datafile2) that are split by chromosome, so I have a number of files that look like this:

datafile1_chr1.txt
datafile1_chr2.txt
datafile1_chr3.txt

datafile2_chr1.txt
datafile2_chr2.txt
datafile2_chr3.txt

I'm trying to write a bit of Bash code that can identify the two datafiles from the same chromosome and run an R script using those two files as variables.

What I have at the moment is very verbose:

Rscript --vanilla matchdata.R datafile1_chr1.txt datafile2_chr1.txt
Rscript --vanilla matchdata.R datafile1_chr2.txt datafile2_chr2.txt
Rscript --vanilla matchdata.R datafile1_chr3.txt datafile2_chr3.txt

Could someone suggest a one line solution for this? I am not sure how I could encorporate variables to help me here.

1 Answer 1

2

A simple loop should be enough:

for i in {1..3}
do
    Rscript --vanilla matchdata.R datafile1_chr${i}.txt datafile2_chr${i}.txt
done

Or, in one line:

for i in {1..3}; do Rscript --vanilla matchdata.R datafile{1,2}_chr${i}.txt; done

Note the second use of brace expansion ({1,2}) - that should expand datafile{1,2}_chr${i}.txt into datafile1_chr${i}.txt datafile2_chr${i}.txt.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.